forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
32 lines (30 loc) · 846 Bytes
/
MinimumWindowSubstring.java
File metadata and controls
32 lines (30 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// TC : O(len(T+S))
class Solution {
public String minWindow(String s, String t) {
int [] freq = new int[128];
for (char c : t.toCharArray()) {
freq[c]++;
}
int start = 0, end = 0, minStart = 0, minLen = Integer.MAX_VALUE, counter = t.length();
while (end < s.length()) {
char endS = s.charAt(end);
if (freq[endS] > 0) {
counter--;
}
freq[endS]--;
end++;
while (counter == 0) {
if (minLen > end - start) {
minLen = end - start;
minStart = start;
}
//System.out.println(s.substring(start, end));
char startS = s.charAt(start);
freq[startS]++;
if (freq[startS] > 0) counter++;
start++;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLen);
}
}