forked from kedebug/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
39 lines (39 loc) · 1.09 KB
/
MinimumWindowSubstring.cpp
File metadata and controls
39 lines (39 loc) · 1.09 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
class Solution {
public:
string minWindow(string S, string T) {
string result("");
map<char, int> needed;
map<char, int> found;
for (int i = 0; i < T.size(); i++) {
needed[T[i]]++;
}
int count = 0;
int minlen = S.size() + 1;
for (int i = 0, j = 0; j < S.size(); j++) {
if (needed[S[j]] == 0) {
continue;
}
found[S[j]]++;
if (found[S[j]] <= needed[S[j]]) {
count++;
}
if (count == T.size()) {
while (i <= j) {
if (found[S[i]] == 0) {
i++;
} else if (found[S[i]] > needed[S[i]]) {
found[S[i]]--;
i++;
} else {
break;
}
}
if (minlen > j - i + 1) {
minlen = j - i + 1;
result = S.substr(i, minlen);
}
}
}
return result;
}
};