-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumWindowSubstring.java
54 lines (53 loc) · 1.59 KB
/
MinimumWindowSubstring.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class Solution {
public String minWindow(String s, String t) {
int tlen = t.length();
if (tlen == 0)
return "";
int[] tmap = new int[128];
for (int i = 0; i < tlen; i++)
tmap[t.charAt(i)]++;
int[] window = new int[128];
int start = 0, end = 0;
int count = 0;
int maxstart = 0;
int maxend = 0;
while (end < s.length()) {
boolean found = false;
for (; end < s.length(); end++) {
char c = s.charAt(end);
if (tmap[c] > 0) {
window[c]++;
if (window[c] <= tmap[c])
count++;
if (count == tlen) {
found = true;
end++;
break;
}
}
}
if (!found)
break;
for (; start < end; start++) {
char c = s.charAt(start);
if (tmap[c] > 0) {
window[c]--;
if (window[c] < tmap[c]) {
if (maxend == 0 || end - start < maxend - maxstart) {
maxstart = start;
maxend = end;
}
count--;
start++;
break;
}
}
}
}
if (maxend > 0) {
return s.substring(maxstart, maxend);
}
else
return "";
}
}