-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumWindowSubstring3.java
More file actions
65 lines (58 loc) · 2.13 KB
/
MinimumWindowSubstring3.java
File metadata and controls
65 lines (58 loc) · 2.13 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
63
64
65
package leetcode.string;
import java.util.HashMap;
import java.util.Map;
public class MinimumWindowSubstring3 {
public String minWindow(String S, String T) {
if(S==null || T==null) return "";
Map<Character,Integer> total = new HashMap<Character,Integer>();
for(int i=0;i<T.length();i++){
char c = T.charAt(i);
if(total.containsKey(c))
total.put(c,total.get(c)+1);
else
total.put(c,1);
}
Map<Character,Integer> covered = new HashMap<Character,Integer>();
int start=0,
end=-1,
cur1=0,
cur2=0,
count=0;
while(cur1<S.length() && cur2<S.length()){
while(cur1<S.length() && !total.containsKey(S.charAt(cur1)))
cur1++;
if(cur1<S.length()){
if(cur2<cur1) cur2=cur1;
if(count<T.length()){
while(cur2<S.length() && !total.containsKey(S.charAt(cur2)))
cur2++;
if(cur2<S.length()){
char c = S.charAt(cur2);
if(!covered.containsKey(c) ){
covered.put(c,1);
count++;
}else{
if(covered.get(c)<total.get(c))
count++;
covered.put(c,covered.get(c)+1);
}
if(count<T.length()) cur2++;
}
}else{
if(start>end || end-start>cur2-cur1){
end=cur2;
start=cur1;
}
char c = S.charAt(cur1);
covered.put(c,covered.get(c)-1);
cur1++;
if(covered.get(c)<total.get(c)){
count--;
cur2++;
}
}
}
}
return S.substring(start,end+1);
}
}