Skip to content

Commit

Permalink
refactory the code to make it a bit readable
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 30, 2019
1 parent db99fe1 commit 2017083
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions algorithms/cpp/minimumWindowSubstring/minimumWindowSubstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,75 +31,82 @@ using namespace std;

#define INT_MAX 2147483647

string minWindow(string S, string T) {
string minWindow(string s, string t) {
string win;
if (S.size()<=0 || T.size()<=0 || T.size() > S.size()) return win;
if (s.size()<=0 || t.size()<=0 || t.size() > s.size()) return win;
/*
* Declare two "hash map" for ASCII chars
* f[]: represents the char found in string S
* m[]: stores the chars in string T
* window[]: represents the char found in string S
* dict[]: stores the chars in string T
*/
const int MAX_CHARS = 256;
int f[MAX_CHARS], m[MAX_CHARS];
int window[MAX_CHARS], dict[MAX_CHARS];

const int NOT_EXISTED = -1;
const int NOT_FOUND = 0;
memset(m, NOT_EXISTED, sizeof(m));
memset(f, NOT_EXISTED, sizeof(f));
memset(dict, NOT_EXISTED, sizeof(dict));
memset(window, NOT_EXISTED, sizeof(window));

/*
* Go through the T, and inital the m[] and f[]
* Go through the T, and inital the dict[] and window[]
* Notes: a same char can be appeared multiple times.
*/
for(int i=0; i<T.size(); i++) {
m[T[i]]==NOT_EXISTED ? m[T[i]]=1 : m[T[i]]++ ;
f[T[i]] = NOT_FOUND;
for(int i=0; i<t.size(); i++) {
dict[t[i]]==NOT_EXISTED ? dict[t[i]]=1 : dict[t[i]]++ ;
window[t[i]] = NOT_FOUND;
}

int start =-1;
int winSize = INT_MAX;
int letterFound = 0;
int begin = 0;
for(int i=0; i<S.size(); i++) {
/* if S[i] is existed in T*/
if ( m[S[i]] != NOT_EXISTED ){
char ch = S[i];
f[ch]++;

/* if one char has been found enough times, then do not do letterFound++ */
if (f[ch] <= m[ch]) {
letterFound++;
}
if ( letterFound >= T.size() ) {
/*
* Find the beginning of the window
* 1) f[S[begin]] == NOT_EXISTED ===> the char at the `begin` is not in T
* 2) f[S[begin]] > m[S[begin]] ===> a same char appeared more than excepted.
*/
while ( f[S[begin]] == NOT_EXISTED || f[S[begin]] > m[S[begin]] ) {
if ( f[S[begin]] > m[S[begin]] ) {
f[S[begin]]--;
}
begin++;
int left = 0;

for(int right=0; right<s.size(); right++) {
if ( dict[s[right]] == NOT_EXISTED ){
continue;
}

/* if s[i] is existed in `t` */
char chr = s[right];
window[chr]++;

/* if one char has been found enough times, then do not do letterFound++ */
if (window[chr] <= dict[chr]) {
letterFound++;
}

if ( letterFound >= t.size() ) {
/*
* Find the left of the window - try to make the window smaller
* 1) windows[S[left]] == NOT_EXISTED ===> the char at the `left` is not in T
* 2) window[S[left]] > dict[S[left]] ===> a same char appeared more than excepted.
*/
char chl = s[left];
while ( window[chl] == NOT_EXISTED || window[chl] > dict[chl] ) {
if (dict[chl] != NOT_EXISTED ) {
//move the left of window
window[chl]--;
// reduce the number of letters found
if (window[chl] < dict[chl] ) letterFound--;
}
/* Calculate the minimized window size */
if(winSize > i - begin + 1){
start = begin;
winSize = i - begin + 1;
}

chl = s[++left];
}

/* Calculate the minimized window size */
if(winSize > right - left + 1){
start = left;
winSize = right - left + 1;
}

}
}

if (start>=0 && winSize>0) {
win = S.substr(start, winSize);
win = s.substr(start, winSize);
}
return win;
}


int main(int argc, char**argv)
{
string S = "ADOBECODEBANC";
Expand Down

0 comments on commit 2017083

Please sign in to comment.