@@ -31,75 +31,82 @@ using namespace std;
31
31
32
32
#define INT_MAX 2147483647
33
33
34
- string minWindow (string S , string T ) {
34
+ string minWindow (string s , string t ) {
35
35
string win;
36
- if (S .size ()<=0 || T .size ()<=0 || T .size () > S .size ()) return win;
36
+ if (s .size ()<=0 || t .size ()<=0 || t .size () > s .size ()) return win;
37
37
/*
38
38
* Declare two "hash map" for ASCII chars
39
- * f []: represents the char found in string S
40
- * m []: stores the chars in string T
39
+ * window []: represents the char found in string S
40
+ * dict []: stores the chars in string T
41
41
*/
42
42
const int MAX_CHARS = 256 ;
43
- int f [MAX_CHARS], m [MAX_CHARS];
44
-
43
+ int window [MAX_CHARS], dict [MAX_CHARS];
44
+
45
45
const int NOT_EXISTED = -1 ;
46
46
const int NOT_FOUND = 0 ;
47
- memset (m , NOT_EXISTED, sizeof (m ));
48
- memset (f , NOT_EXISTED, sizeof (f ));
47
+ memset (dict , NOT_EXISTED, sizeof (dict ));
48
+ memset (window , NOT_EXISTED, sizeof (window ));
49
49
50
50
/*
51
- * Go through the T, and inital the m [] and f []
51
+ * Go through the T, and inital the dict [] and window []
52
52
* Notes: a same char can be appeared multiple times.
53
53
*/
54
- for (int i=0 ; i<T .size (); i++) {
55
- m[T [i]]==NOT_EXISTED ? m[T [i]]=1 : m[T [i]]++ ;
56
- f[T [i]] = NOT_FOUND;
54
+ for (int i=0 ; i<t .size (); i++) {
55
+ dict[t [i]]==NOT_EXISTED ? dict[t [i]]=1 : dict[t [i]]++ ;
56
+ window[t [i]] = NOT_FOUND;
57
57
}
58
58
59
59
int start =-1 ;
60
60
int winSize = INT_MAX;
61
61
int letterFound = 0 ;
62
- int begin = 0 ;
63
- for (int i=0 ; i<S.size (); i++) {
64
- /* if S[i] is existed in T*/
65
- if ( m[S[i]] != NOT_EXISTED ){
66
- char ch = S[i];
67
- f[ch]++;
68
-
69
- /* if one char has been found enough times, then do not do letterFound++ */
70
- if (f[ch] <= m[ch]) {
71
- letterFound++;
72
- }
73
- if ( letterFound >= T.size () ) {
74
- /*
75
- * Find the beginning of the window
76
- * 1) f[S[begin]] == NOT_EXISTED ===> the char at the `begin` is not in T
77
- * 2) f[S[begin]] > m[S[begin]] ===> a same char appeared more than excepted.
78
- */
79
- while ( f[S[begin]] == NOT_EXISTED || f[S[begin]] > m[S[begin]] ) {
80
- if ( f[S[begin]] > m[S[begin]] ) {
81
- f[S[begin]]--;
82
- }
83
- begin++;
62
+ int left = 0 ;
63
+
64
+ for (int right=0 ; right<s.size (); right++) {
65
+ if ( dict[s[right]] == NOT_EXISTED ){
66
+ continue ;
67
+ }
68
+
69
+ /* if s[i] is existed in `t` */
70
+ char chr = s[right];
71
+ window[chr]++;
72
+
73
+ /* if one char has been found enough times, then do not do letterFound++ */
74
+ if (window[chr] <= dict[chr]) {
75
+ letterFound++;
76
+ }
77
+
78
+ if ( letterFound >= t.size () ) {
79
+ /*
80
+ * Find the left of the window - try to make the window smaller
81
+ * 1) windows[S[left]] == NOT_EXISTED ===> the char at the `left` is not in T
82
+ * 2) window[S[left]] > dict[S[left]] ===> a same char appeared more than excepted.
83
+ */
84
+ char chl = s[left];
85
+ while ( window[chl] == NOT_EXISTED || window[chl] > dict[chl] ) {
86
+ if (dict[chl] != NOT_EXISTED ) {
87
+ // move the left of window
88
+ window[chl]--;
89
+ // reduce the number of letters found
90
+ if (window[chl] < dict[chl] ) letterFound--;
84
91
}
85
- /* Calculate the minimized window size */
86
- if (winSize > i - begin + 1 ){
87
- start = begin;
88
- winSize = i - begin + 1 ;
89
- }
90
-
92
+ chl = s[++left];
93
+ }
94
+
95
+ /* Calculate the minimized window size */
96
+ if (winSize > right - left + 1 ){
97
+ start = left;
98
+ winSize = right - left + 1 ;
91
99
}
92
-
100
+
93
101
}
94
102
}
95
103
96
104
if (start>=0 && winSize>0 ) {
97
- win = S .substr (start, winSize);
105
+ win = s .substr (start, winSize);
98
106
}
99
107
return win;
100
108
}
101
109
102
-
103
110
int main (int argc, char **argv)
104
111
{
105
112
string S = " ADOBECODEBANC" ;
0 commit comments