-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cc
More file actions
54 lines (49 loc) · 1.93 KB
/
MinimumWindowSubstring.cc
File metadata and controls
54 lines (49 loc) · 1.93 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
51
52
53
54
namespace MinimumWindowSubString {
class Solution {
public:
string minWindow(string S, string T) { // Note: T could contain duplicated entries, such as "aab"
if (T.empty()) return "";
int alphaSet[256];
fill_n(alphaSet, 256, 0);
for (int i = 0; i < T.size(); i++) {
alphaSet[T[i]]++;
}
int curAlphaSet[256];
fill_n(curAlphaSet, 256, 0);
int end = 0;
int start = 0;
int minStart = 0;
int minLen = INT_MAX;
int uniqueCount = 0;
while (end < S.size()) {
// move end forward
char charEnd = S[end];
if (alphaSet[charEnd] > 0) {
curAlphaSet[charEnd]++;
if (curAlphaSet[charEnd] <= alphaSet[charEnd]) uniqueCount++;
if (uniqueCount == T.size()) {
// move forward start
while (start <= S.size() - T.size()) {
if (minLen > end - start + 1) {
minLen = end - start + 1;
minStart = start;
}
char charStart = S[start];
if (alphaSet[charStart] > 0) {
curAlphaSet[charStart]--;
if (curAlphaSet[charStart] < alphaSet[charStart]) {
uniqueCount--;
start++;
break;
}
}
start++;
}
}
}
end++;
}
return minLen > S.size()? "": S.substr(minStart, minLen);
}
};
}