forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumwindowsubstring.java
More file actions
executable file
·62 lines (60 loc) · 1.83 KB
/
minimumwindowsubstring.java
File metadata and controls
executable file
·62 lines (60 loc) · 1.83 KB
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
55
56
57
58
59
60
61
62
public class Solution {
public String minWindow(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
int start = 0;
int end = 0;
int n =0;
int min = Integer.MAX_VALUE;
int minstart = 0;
int minend = 0;
HashSet<Character> hs = new HashSet<Character>();
int count[] = new int[300];
int sta[] = new int[300];
for(int i=0;i<T.length();i++){
hs.add(T.charAt(i));
sta[T.charAt(i)+0]++;
}
int target = hs.size();
boolean found = false;
while(end<S.length()){
while(n<target && end < S.length()){
if(!hs.contains(S.charAt(end))){
end++;
}else{
count[S.charAt(end)+0]++;
if(count[S.charAt(end)+0]==sta[S.charAt(end)+0]){
n++;
}
end++;
}
}
if(min>end+1-start && n==target){
found = true;
min = end+1-start;
minend = end;
minstart = start;
}
while(n==target && start<=end){
if(!hs.contains(S.charAt(start))){
start++;
}else{
if(count[S.charAt(start)+0]==sta[S.charAt(start)+0]){
n--;
}
count[S.charAt(start)+0]--;
start++;
}
}
if(min>end+2-start){
min = end+2-start;
minend = end;
minstart = start-1;
}
}
if(!found){
return "";
}
return S.substring(minstart,minend);
}
}