Skip to content

Commit 10d45dd

Browse files
76. Minimum Window Substring (java)
1 parent 6607ca3 commit 10d45dd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String minWindow(String s, String t) {
3+
int[] count = new int['z' - 'A' + 1];
4+
int uniq = 0;
5+
for (int i = 0; i < t.length(); ++i) {
6+
if (++count[t.charAt(i) - 'A'] == 1) uniq++;
7+
}
8+
int found = 0,i = 0,j = 0;
9+
int minLen = Integer.MAX_VALUE;
10+
int minJ = Integer.MAX_VALUE;
11+
while (found < uniq) {
12+
while (i < s.length()) {
13+
if (found >= uniq) break;
14+
if (--count[s.charAt(i) - 'A'] == 0) found++;
15+
i++;
16+
}
17+
if (found < uniq) break;
18+
while (j < i && count[s.charAt(j) - 'A'] < 0) count[s.charAt(j++) - 'A']++;
19+
if (i - j < minLen) {
20+
minLen = i - j;
21+
minJ = j;
22+
}
23+
count[s.charAt(j++) - 'A']++;
24+
found--;
25+
}
26+
return minLen < Integer.MAX_VALUE ? s.substring(minJ, minJ + minLen) : "";
27+
}
28+
}

0 commit comments

Comments
 (0)