-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Window Substring.txt
More file actions
55 lines (47 loc) · 1.55 KB
/
Minimum Window Substring.txt
File metadata and controls
55 lines (47 loc) · 1.55 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
class Solution {
public String minWindow(String s, String t) {
HashMap<Character,Integer> map1=new HashMap<Character,Integer>();
for(int i=0;i<t.length();i++){
char ch=t.charAt(i);
map1.put(ch,map1.getOrDefault(ch,0)+1);
}
String res="";
int i=-1,j=-1;
int dmct=t.length();
int mct=0;
HashMap<Character,Integer> map2=new HashMap<Character,Integer>();
while(true){
boolean flag1=false,flag2=false;
while(i<s.length()-1 && mct!=dmct){
i++;
char ch=s.charAt(i);
map2.put(ch,map2.getOrDefault(ch,0)+1);
if(map2.getOrDefault(ch,0)<=map1.getOrDefault(ch,0)){
mct++;
}
flag1=true;
}
while(j<i && mct==dmct){
String pans=s.substring(j+1,i+1);
if(res.length()==0 || pans.length()<res.length()){
res=pans;
}
j++;
char ch=s.charAt(j);
if(map2.get(ch)==1){
map2.remove(ch);
}else{
map2.put(ch,map2.get(ch)-1);
}
if(map2.getOrDefault(ch,0)<map1.getOrDefault(ch,0)){
mct--;
}
flag2=true;
}
if(flag1==false && flag2==false){
break;
}
}
return res;
}
}