File tree 1 file changed +28
-0
lines changed
solution/0076.Minimum Window Substring
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String minWindow (String s , String t ) {
3
+ int [] count = new int ['z' - 'A' + 1 ];
4
+ int uniq = 0 ;
5
+ for (int i = 0 ; i < t .length (); ++i ) {
6
+ if (++count [t .charAt (i ) - 'A' ] == 1 ) uniq ++;
7
+ }
8
+ int found = 0 ,i = 0 ,j = 0 ;
9
+ int minLen = Integer .MAX_VALUE ;
10
+ int minJ = Integer .MAX_VALUE ;
11
+ while (found < uniq ) {
12
+ while (i < s .length ()) {
13
+ if (found >= uniq ) break ;
14
+ if (--count [s .charAt (i ) - 'A' ] == 0 ) found ++;
15
+ i ++;
16
+ }
17
+ if (found < uniq ) break ;
18
+ while (j < i && count [s .charAt (j ) - 'A' ] < 0 ) count [s .charAt (j ++) - 'A' ]++;
19
+ if (i - j < minLen ) {
20
+ minLen = i - j ;
21
+ minJ = j ;
22
+ }
23
+ count [s .charAt (j ++) - 'A' ]++;
24
+ found --;
25
+ }
26
+ return minLen < Integer .MAX_VALUE ? s .substring (minJ , minJ + minLen ) : "" ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments