-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Window Substring.cpp
More file actions
33 lines (33 loc) · 1.05 KB
/
Minimum Window Substring.cpp
File metadata and controls
33 lines (33 loc) · 1.05 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
// Double-Pointer Solution
class Solution {
public:
string minWindow(string s, string t) {
int n = s.size(), m = t.size();
unordered_map<char, int> dict, visit;
int dcount = 0, vcount = 0;
for (int i = 0; i < m; ++i) dict[t[i]] ++;
int l = 0, h = 0, retStart = 0, retLen = n + 1;
while (h < n) {
for (; h < n && vcount < m; ++h) {
if (dict.find(s[h]) != dict.end()) {
if (visit[s[h]] < dict[s[h]]) vcount ++;
visit[s[h]] ++;
}
}
if (h == n && vcount < m) break;
for (; l < h; ++l) {
if (dict.find(s[l]) != dict.end()) {
if (visit[s[l]] <= dict[s[l]]) break;
visit[s[l]] --;
}
}
if (h - l < retLen) {
retStart = l;
retLen = h - l;
}
visit[s[l++]] --;
vcount --;
}
return s.substr(retStart, retLen > n ? 0 : retLen);
}
};