Skip to content

Commit

Permalink
Minimum Window Substring
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyKim9401 committed Oct 10, 2024
1 parent 6d4765e commit 89cce77
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions minimum-window-substring/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// TC: O(n)
// using two pointer lef and right, it visits all elements only once each.
// SC: O(n + m)
// 2 hashmap used for checking the given Strings s and t, n is the size of s, m is the size of m
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> map = new HashMap<>();

for (char c : t.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}

int required = map.size();
int formed = 0;

int left = 0;
int right = 0;
int[] ans = {-1, 0, 0};

Map<Character, Integer> windowCounts = new HashMap<>();

while (right < s.length()) {
char c = s.charAt(right);
windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1);

if (map.containsKey(c) &&
windowCounts.get(c).intValue() == map.get(c).intValue()) {
formed += 1;
}

while (left <= right && formed == required) {
c = s.charAt(left);

if (ans[0] == -1 || right - left + 1 < ans[0]) {
ans[0] = right - left + 1;
ans[1] = left;
ans[2] = right;
}

windowCounts.put(c, windowCounts.get(c) - 1);
if (map.containsKey(c) &&
windowCounts.get(c).intValue() < map.get(c).intValue()) {
formed -= 1;
}

left += 1;
}
right += 1;
}
return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1);
}
}

0 comments on commit 89cce77

Please sign in to comment.