1
+ // Runtime: 98 ms (Top 16.21%) | Memory: 43.4 MB (Top 87.12%)
1
2
class Solution {
2
3
public String minWindow (String s , String t ) {
3
4
HashMap <Character , Integer > child = new HashMap <>();
4
5
HashMap <Character , Integer > parent = new HashMap <>();
5
-
6
+
6
7
int left = -1 , right = -1 , match = 0 ;
7
8
String window = "" ;
8
-
9
+
9
10
for (int i = 0 ; i < t .length (); i ++){
10
- char c = t .charAt (i );
11
- child .put (c , child .getOrDefault (c , 0 ) + 1 ); //Child frequency map
11
+ char c = t .charAt (i );
12
+ child .put (c , child .getOrDefault (c , 0 ) + 1 ); //Child frequency map
12
13
}
13
-
14
+
14
15
while (true ){
15
16
boolean f1 = false , f2 = false ;
16
-
17
+
17
18
while (right < s .length () - 1 && match < t .length ()){
18
19
right ++;
19
20
char c = s .charAt (right );
20
- parent .put (c , parent .getOrDefault (c , 0 ) + 1 ); // Acquiring characters till
21
- if (parent .getOrDefault (c , 0 ) <= child .getOrDefault (c , 0 )) // match count is equal
21
+ parent .put (c , parent .getOrDefault (c , 0 ) + 1 ); // Acquiring characters till
22
+ if (parent .getOrDefault (c , 0 ) <= child .getOrDefault (c , 0 )) // match count is equal
22
23
match ++;
23
-
24
+
24
25
f1 = true ;
25
26
}
26
27
while (left < right && match == t .length ()){
27
28
String potstring = s .substring (left + 1 , right + 1 );
28
29
if (window .length () == 0 || window .length () > potstring .length ())
29
- window = potstring ; //Calculating length of window
30
-
30
+ window = potstring ; //Calculating length of window
31
+
31
32
left ++;
32
33
char c = s .charAt (left );
33
34
parent .put (c , parent .getOrDefault (c , 0 ) - 1 );
34
- if (parent .get (c ) == 0 ) //Releasing characters by
35
- parent .remove (c ); //left pointer for finding smallest window
36
-
35
+ if (parent .get (c ) == 0 ) //Releasing characters by
36
+ parent .remove (c ); //left pointer for finding smallest window
37
+
37
38
if (parent .getOrDefault (c , 0 ) < child .getOrDefault (c , 0 ))
38
39
match --;
39
-
40
+
40
41
f2 = true ;
41
42
}
42
-
43
+
43
44
if (f1 == false && f2 == false )
44
45
break ;
45
46
}
46
-
47
-
47
+
48
48
return window ;
49
49
}
50
- }
50
+ }
0 commit comments