-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.minimum-window-substring.java
More file actions
54 lines (44 loc) · 1.63 KB
/
76.minimum-window-substring.java
File metadata and controls
54 lines (44 loc) · 1.63 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
class Solution {
public String minWindow(String s, String t) {
if(s.length() == 0 || t.length() == 0 || s.length() < t.length()){
return "";
}
Map<Character, Integer> mapT = new HashMap<>();
for(int i=0; i< t.length(); i++){
mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i),0) + 1);
}
int required = mapT.size();
int l = 0, r =0;
int create = 0;
int [] ans = {-1, 0, 0};
Map<Character, Integer> subStringMap = new HashMap<>();
while(r<s.length()){
char c = s.charAt(r);
int count = subStringMap.getOrDefault(c,0);
subStringMap.put(c, count + 1);
if(mapT.containsKey(c) && subStringMap.get(c).intValue() == mapT.get(c).intValue()){
create++;
}
while(l <= r && required == create){
c = s.charAt(l);
if(ans[0] == -1 || ans[0] >= r-l+1){
ans[0] = r -l +1;
ans[1] = l;
ans[2] = r;
}
subStringMap.put(c,subStringMap.get(c) - 1);
if(mapT.containsKey(c) && subStringMap.get(c).intValue() < mapT.get(c).intValue()){
create--;
}
l++;
}
r++;
}
if(ans[0] == -1){
return "";
}
else{
return s.substring(ans[1], ans[2]+1);
}
}
}