@@ -12,97 +12,49 @@ public class SmallestWindowContaingAllCharacters {
12
12
13
13
public String minWindow (String s , String t ) {
14
14
Map <Character , Integer > countMap = new HashMap <>();
15
- for (char s1 : t .toCharArray ()) {
16
- countMap .compute (s1 , (key , val ) -> {
17
- if (val == null ) {
18
- return 1 ;
19
- }
20
- return val + 1 ;
21
- });
22
- }
23
-
24
- Map <Character , Integer > foundMap = new HashMap <>();
25
- for (char s1 : t .toCharArray ()) {
26
- foundMap .put (s1 , 0 );
27
- }
28
-
29
- int startWindow ;
30
- int charsFound = 0 ;
31
- for (startWindow = 0 ; startWindow < s .length (); startWindow ++) {
32
- if (countMap .containsKey (s .charAt (startWindow ))) {
33
- break ;
15
+ for (char ch : t .toCharArray ()) {
16
+ Integer val = countMap .get (ch );
17
+ if (val == null ) {
18
+ val = 0 ;
34
19
}
20
+ countMap .put (ch , val + 1 );
35
21
}
36
- int minWindowStart = Integer .MIN_VALUE ;
37
- int minWindowEnd = -1 ;
38
- int totalCharacters = t .length ();
39
- for (int i = startWindow ; i < s .length (); i ++) {
40
- char ch = s .charAt (i );
41
- Integer count = countMap .get (ch );
42
- if (count != null ) {
43
- Integer actualCount = foundMap .get (ch );
44
- foundMap .put (ch , actualCount + 1 );
45
- if (actualCount < count ) {
46
- charsFound ++;
47
- if (charsFound == totalCharacters ) {
48
- if (i - startWindow < minWindowEnd - minWindowStart ) {
49
- minWindowStart = startWindow ;
50
- minWindowEnd = i ;
51
- }
52
- ShrinkResult sr = shrinkWindow (countMap , foundMap , startWindow , s , i );
53
- if (sr .fullStartWindow != -1 ) {
54
- if (i - sr .fullStartWindow < minWindowEnd - minWindowStart ) {
55
- minWindowStart = sr .fullStartWindow ;
56
- minWindowEnd = i ;
57
- }
58
- }
59
- startWindow = sr .startWindow ;
60
- charsFound --;
61
- }
62
- }
22
+ int start = 0 ;
23
+ int currLen = t .length ();
24
+ int minWindow = Integer .MAX_VALUE ;
25
+ int minStart = 0 ;
26
+ int i = 0 ;
27
+ while (i < s .length ()) {
28
+ Integer val = countMap .get (s .charAt (i ));
29
+ if (val == null ) {
30
+ i ++;
31
+ continue ;
63
32
}
64
- }
65
- if (minWindowEnd == -1 ) {
66
- return "" ;
67
- } else {
68
- return s .substring (minWindowStart , minWindowEnd + 1 );
69
- }
70
- }
71
-
72
- class ShrinkResult {
73
- int startWindow ;
74
- int fullStartWindow ;
75
- }
76
-
77
- private ShrinkResult shrinkWindow (Map <Character , Integer > countMap , Map <Character , Integer > foundMap ,
78
- int startWindow , String s , int end ) {
79
- boolean firstViolation = false ;
80
- int diff = -1 ;
81
- int i ;
82
- for (i = startWindow ; i <= end ; i ++) {
83
- if (!firstViolation ) {
84
- diff = i ;
33
+ if (val > 0 ) {
34
+ currLen --;
85
35
}
86
- char ch = s .charAt (i );
87
- Integer count = countMap .get (ch );
88
- if (count != null ) {
89
- int actualCount = foundMap .get (ch );
90
- if (firstViolation ) {
91
- if (actualCount <= count ) {
36
+ val --;
37
+ countMap .put (s .charAt (i ), val );
38
+ while (currLen == 0 ) {
39
+ if (minWindow > i - start + 1 ) {
40
+ minWindow = i - start + 1 ;
41
+ minStart = start ;
42
+ }
43
+ Integer val1 = countMap .get (s .charAt (start ));
44
+ if (val1 != null ) {
45
+ if (val1 == 0 ) {
92
46
break ;
93
- }
94
- } else {
95
- if (actualCount == count ) {
96
- firstViolation = true ;
47
+ } else {
48
+ val1 ++;
49
+ countMap .put (s .charAt (start ), val1 );
97
50
}
98
51
}
99
- foundMap . compute ( ch , ( key , val ) -> val - 1 ) ;
52
+ start ++ ;
100
53
}
54
+ i ++;
101
55
}
102
- ShrinkResult sr = new ShrinkResult ();
103
- sr .fullStartWindow = diff ;
104
- sr .startWindow = i ;
105
- return sr ;
56
+
57
+ return minWindow != Integer .MAX_VALUE ? s .substring (minStart , minStart + minWindow ) : "" ;
106
58
}
107
59
108
60
public static void main (String args []) {
0 commit comments