-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM90.java
More file actions
39 lines (37 loc) · 1017 Bytes
/
BM90.java
File metadata and controls
39 lines (37 loc) · 1017 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
32
33
34
35
36
37
38
39
package NiukeTOP101;
public class BM90 {
public String minWindow (String S, String T) {
int cnt = S.length() + 1;
int[] hash = new int[128];
for (int i = 0; i < T.length(); i++) {
hash[T.charAt(i)]--;
}
int slow = 0, fast = 0;
int left = -1, right = -1;
for (fast = 0; fast < S.length(); fast++) {
hash[S.charAt(fast)]++;
while (check(hash)){
//取最优解
if(cnt > fast - slow + 1){
cnt = fast - slow + 1;
left = slow;
right = fast;
}
hash[S.charAt(slow)]--;
slow ++;
}
}
if(left == -1){
return "";
}
return S.substring(left, right + 1);
}
private boolean check(int[] hash) {
for (int j : hash) {
if (j < 0) {
return false;
}
}
return true;
}
}