forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
28 lines (28 loc) · 963 Bytes
/
Solution.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
class Solution {
public String minWindow(String s, String t) {
int[] count = new int['z' - 'A' + 1];
int uniq = 0;
for (int i = 0; i < t.length(); ++i) {
if (++count[t.charAt(i) - 'A'] == 1) uniq++;
}
int found = 0,i = 0,j = 0;
int minLen = Integer.MAX_VALUE;
int minJ = Integer.MAX_VALUE;
while (found < uniq) {
while (i < s.length()) {
if (found >= uniq) break;
if (--count[s.charAt(i) - 'A'] == 0) found++;
i++;
}
if (found < uniq) break;
while (j < i && count[s.charAt(j) - 'A'] < 0) count[s.charAt(j++) - 'A']++;
if (i - j < minLen) {
minLen = i - j;
minJ = j;
}
count[s.charAt(j++) - 'A']++;
found--;
}
return minLen < Integer.MAX_VALUE ? s.substring(minJ, minJ + minLen) : "";
}
}