forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
50 lines (48 loc) · 1.46 KB
/
MinimumWindowSubstring.cpp
File metadata and controls
50 lines (48 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
string minWindow(string S, string T) {
int slen = (int)(S.length());
int count[256];
int countT[256];
int totalCount = 0;
int totalCountT = 0;
for(int i=0;i<256;i++) {
count[i] = 0;
countT[i] = 0;
}
for(int i=0;i<(int)T.length();i++) {
countT[T[i]]++;
totalCountT++;
}
int start = 0;
int end = 0;
int minWindowLen = 0x7fffffff;
string minWidowStr = "";
while(end<slen) {
if(countT[S[end]] > 0) {
count[S[end]]++;
if(count[S[end]] <= countT[S[end]]) {
totalCount++;
}
if(totalCount == totalCountT) {
while(start<=end) {
if(countT[S[start]] ==0 ) {
start++;
} else if(count[S[start]] > countT[S[start]]) {
count[S[start]]--;
start++;
} else {
break;
}
}
if(minWindowLen > end-start+1) {
minWindowLen = end-start+1;
minWidowStr = S.substr(start, minWindowLen);
}
}
}
end++;
}
return minWidowStr;
}
};