Skip to content

Commit 9c39fd9

Browse files
committed
refactory the code to make it a bit readable
1 parent a833183 commit 9c39fd9

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

algorithms/cpp/minimumWindowSubstring/minimumWindowSubstring.cpp

+50-43
Original file line numberDiff line numberDiff line change
@@ -31,75 +31,82 @@ using namespace std;
3131

3232
#define INT_MAX 2147483647
3333

34-
string minWindow(string S, string T) {
34+
string minWindow(string s, string t) {
3535
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;
3737
/*
3838
* 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
4141
*/
4242
const int MAX_CHARS = 256;
43-
int f[MAX_CHARS], m[MAX_CHARS];
44-
43+
int window[MAX_CHARS], dict[MAX_CHARS];
44+
4545
const int NOT_EXISTED = -1;
4646
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));
4949

5050
/*
51-
* Go through the T, and inital the m[] and f[]
51+
* Go through the T, and inital the dict[] and window[]
5252
* Notes: a same char can be appeared multiple times.
5353
*/
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;
5757
}
5858

5959
int start =-1;
6060
int winSize = INT_MAX;
6161
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--;
8491
}
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;
9199
}
92-
100+
93101
}
94102
}
95103

96104
if (start>=0 && winSize>0) {
97-
win = S.substr(start, winSize);
105+
win = s.substr(start, winSize);
98106
}
99107
return win;
100108
}
101109

102-
103110
int main(int argc, char**argv)
104111
{
105112
string S = "ADOBECODEBANC";

0 commit comments

Comments
 (0)