forked from geemaple/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.minimum-window-substring.cpp
More file actions
46 lines (37 loc) · 1.22 KB
/
76.minimum-window-substring.cpp
File metadata and controls
46 lines (37 loc) · 1.22 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
class Solution {
public:
string minWindow(string s, string t) {
if (s.size() < t.size()){
return "";
}
unordered_map<char, int> map;
int missing = t.size();
for (auto i = 0; i < t.size(); ++i) {
map[t[i]] += 1;
}
pair<int, int> res = make_pair(1, INT_MAX);
int start = 0;
for (auto end = 0; end < s.size(); ++end){
if (map.count(s[end]) > 0){
if (map[s[end]] > 0) {
missing -= 1;
}
map[s[end]] -= 1;
}
while (missing == 0){
if (res.second - res.first > end + 1 - start){
res = make_pair(start, end + 1);
}
// remove start
if (map.count(s[start]) > 0) {
if (map[s[start]] == 0) {
missing += 1;
}
map[s[start]] += 1;
}
start += 1;
}
}
return (res.second != INT_MAX)? s.substr(res.first, res.second - res.first): "";
}
};