-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path0076-minimum-window-substring.cpp
More file actions
52 lines (50 loc) · 1.58 KB
/
Copy path0076-minimum-window-substring.cpp
File metadata and controls
52 lines (50 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
46
47
48
49
50
51
52
// class Solution {
// public:
// string minWindow(string S, string T) {
// if (S.size() < T.size()) return "";
// vector<int> t_need(128, 0), t_has(128, 0);
// for (auto t: T) t_need[t]++;
// auto check = [&]() {
// for (auto i = 0; i < t_need.size(); i++)
// if (t_has[i] < t_need[i]) return false;
// return true;
// };
// pair<int, int> keep {0, INT_MAX};
// for (auto i = 0, j = 0; i < S.size(); i++) {
// t_has[S[i]]++;
// while (check()) {
// auto dist_before = keep.second - keep.first;
// if (dist_before > (i - j)) keep = {j, i};
// t_has[S[j++]]--;
// }
// }
// auto [from, to] = keep;
// if (to >= S.size()) return "";
// return S.substr(keep.first, keep.second - keep.first + 1);
// }
// };
class Solution {
public:
string minWindow(string s, string t) {
vector<int> map(128,0);
for (char c : t) {
map[c]++;
}
int counter = t.size(), begin = 0, end = 0, d = INT_MAX, head = 0;
while (end < s.size()){
if (map[s[end++]]-- > 0) {
counter--;
}
while (counter == 0) {
if (end - begin < d) {
head = begin;
d = end - head;
}
if (map[s[begin++]]++ == 0) {
counter++;
}
}
}
return d == INT_MAX ? "" : s.substr(head, d);
}
};