-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathminimum-window-substring.cpp
More file actions
67 lines (58 loc) · 1.53 KB
/
minimum-window-substring.cpp
File metadata and controls
67 lines (58 loc) · 1.53 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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "leetcode.h"
class Solution {
public:
string minWindow(string S, string T) {
if (S.size() == 0 || T.size() == 0) return string("");
int hasFound[256] = {0};
int toFound[256] = {0};
// count the number of requried chars in T
for (char c : T) {
++toFound[c];
}
int count = 0;
int minLen = numeric_limits<int>::max();
int minBegin = -1;
for (int begin = 0, end = 0; end < S.size(); ++end) {
// advance end pointer and see if found a valid window
if (toFound[S[end]] == 0) {
continue;
}
// only count when the requirement is not met
if (hasFound[S[end]] < toFound[S[end]]) {
++count;
}
++hasFound[S[end]];
// found a valid window, advance begin pointer
if (count >= T.size()) {
while (toFound[S[begin]] == 0 ||
hasFound[S[begin]] > toFound[S[begin]]) {
if (toFound[S[begin]] > 0) {
--hasFound[S[begin]];
}
++begin;
}
// new window
int newLen = end - begin + 1;
if (newLen < minLen) {
minLen = newLen;
minBegin = begin;
// cout << "Find new window: " << begin << ", " << end << '\n';
}
}
}
string result;
if (count >= T.size()) {
result = S.substr(minBegin, minLen);
}
return result;
}
};
int main() {
// string S("acbbaca");
// string T("aba");
string S("ADOBECODEBANC");
string T("BAC");
Solution sol;
cout << sol.minWindow(S, T) << '\n';
return 0;
}