File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
LeetCode/76. Minimum Window Substring Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/minimum-window-substring/
2
+
3
+ class Solution {
4
+ public String minWindow (String s , String t )
5
+ {
6
+ if (s .length () == 0 || t .length () == 0 )
7
+ return "" ;
8
+ HashMap <Character ,Integer > map = new HashMap <>();
9
+ int n = s .length (),m =t .length ();
10
+
11
+ for (int i =0 ; i <m ;i ++)
12
+ map .put (t .charAt (i ) , map .getOrDefault (t .charAt (i ), 0 )+1 );
13
+
14
+ int count = map .size ();
15
+ int start =0 ,end =0 ,min =Integer .MAX_VALUE ;
16
+ String substring = "" ;
17
+
18
+ while (end < n )
19
+ {
20
+ char ch = s .charAt (end );
21
+ if (map .containsKey (ch ))
22
+ {
23
+ map .put (ch , map .get (ch ) -1 );
24
+ if (map .get (ch ) == 0 )
25
+ count --;
26
+ }
27
+ if (count > 0 )
28
+ end ++;
29
+
30
+ else if (count == 0 )
31
+ {
32
+ while (count == 0 )
33
+ {
34
+ if (end -start +1 < min )
35
+ {
36
+ min = end -start +1 ;
37
+ substring = s .substring (start ,end +1 );
38
+ }
39
+ char temp = s .charAt (start );
40
+ if (map .containsKey (temp ))
41
+ {
42
+ map .put (temp , map .get (temp )+1 );
43
+ if (map .get (temp ) == 1 )
44
+ count ++;
45
+ }
46
+ start ++;
47
+ }
48
+ end ++;
49
+ }
50
+ }
51
+ return substring ;
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments