forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (29 loc) · 829 Bytes
/
Solution.cpp
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
class Solution {
public:
string minWindow(string s, string t){
unordered_map<char, int> m;
int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size();
for (auto c: t)
m[c]++;
while (end < size) {
if (m[s[end]] > 0)
counter--;
m[s[end]]--;
end++;
while (counter == 0) {
if (end - begin < minlen) {
minStart = begin;
minlen = end - begin;
}
m[s[begin]]++;
if (m[s[begin]] > 0)
counter++;
begin++;
}
}
if (minlen != INT_MAX) {
return s.substr(minStart, minlen);
}
return "";
}
};