forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
31 lines (30 loc) · 976 Bytes
/
MinimumWindowSubstring.java
File metadata and controls
31 lines (30 loc) · 976 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
public class MinimumWindowSubstring {
// 耗时6ms,时间复杂度O(n)
public String minWindow(String s, String t) {
int[] tc = new int[256], sc = new int[256];
for (char c : t.toCharArray()) {
tc[c]++;
}
int count = 0, min = Integer.MAX_VALUE, start = 0;
for (int i = 0, j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (++sc[c] <= tc[c]) {
++count;
}
if (count == t.length()) {
for ( ; i < j; i++) {
char cc = s.charAt(i);
if (sc[cc] <= tc[cc]) {
break;
}
sc[cc]--;
}
if (j - i + 1 < min) {
min = j - i + 1;
start = i;
}
}
}
return min == Integer.MAX_VALUE ? "" : s.substring(start, start + min);
}
}