-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay66.cpp
More file actions
56 lines (41 loc) · 1.25 KB
/
Day66.cpp
File metadata and controls
56 lines (41 loc) · 1.25 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
class Solution {
public:
string minWindow(string s, string t) {
if (s.empty() || t.empty()) {
return "";
}
unordered_map<char, int> mp;
for (char ch : t) {
int cnt = mp[ch];
mp[ch] = cnt + 1;
}
int required = mp.size();
int l = 0, r = 0;
int f = 0;
unordered_map<char, int> windowCounts;
int ans[3] = { -1, 0, 0 };
while (r < s.length()) {
char c = s[r];
int cnt = windowCounts[c];
windowCounts[c] = cnt + 1;
if (mp.find(c) != mp.end() && windowCounts[c] == mp[c]) {
f++;
}
while (l <= r && f == required) {
c = s[l];
if (ans[0] == -1 || r - l + 1 < ans[0]) {
ans[0] = r - l + 1;
ans[1] = l;
ans[2] = r;
}
windowCounts[c]--;
if (mp.find(c) != mp.end() && windowCounts[c] < mp[c]) {
f--;
}
l++;
}
r++;
}
return ans[0] == -1 ? "" : s.substr(ans[1], ans[0]);
}
};