-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path076.cpp
More file actions
executable file
·45 lines (45 loc) · 1.58 KB
/
076.cpp
File metadata and controls
executable file
·45 lines (45 loc) · 1.58 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
class Solution
{
public:
string minWindow(string s, string t)
{
string res;
map<char, int> total, cnt;
for (auto c : t) {
++total[c];
}
int i = 0, j = 0, n = (int)s.size();
size_t sum = 0;
for (i = 0; i < n && sum < t.size(); ++i) {
char c = s[i];
++cnt[c];
if (cnt[c] <= total[c]) {
++sum;
}
}
while (cnt[s[j]] > total[s[j]]) {
--cnt[s[j]];
++j;
}
if (sum < t.size()) {
return res;
}
res = s.substr(j, i - j);
for (; i < n; ++i) {
char c = s[i];
++cnt[c];
if (cnt[c] <= total[c]) {
++sum;
} else if (c == s[j]) {
while (cnt[s[j]] > total[s[j]]) {
--cnt[s[j]];
++j;
}
}
if (res.size() > (i - j + 1)) {
res = s.substr(j, i - j + 1);
}
}
return res;
}
};