-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumWindow.java
29 lines (28 loc) · 925 Bytes
/
MinimumWindow.java
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
public class MinimumWindow {
public String minWindow(String s, String t) {
int[] map = new int[128];
for (char c : t.toCharArray()) {
map[c]++;
}
int start = 0, end = 0, minStart = 0, minLen = Integer.MAX_VALUE, counter = t.length();
while (end < s.length()) {
final char c1 = s.charAt(end);
if (map[c1] > 0)
counter--;
map[c1]--;
end++;
while (counter == 0) {
if (minLen > end - start) {
minLen = end - start;
minStart = start;
}
final char c2 = s.charAt(start);
map[c2]++;
if (map[c2] > 0)
counter++;
start++;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLen);
}
}