-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path76+Minimum Window Substring.cpp
More file actions
44 lines (34 loc) · 1.1 KB
/
76+Minimum Window Substring.cpp
File metadata and controls
44 lines (34 loc) · 1.1 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
class Solution {
public:
unordered_map<char, int> umaps, umapt;
bool check() {
for (auto c : umapt) {
if (umaps[c.first] < c.second) {
return false;
}
}
return true;
}
string minWindow(string s, string t) {
for (auto c : t)
umapt[c]++;
int left = 0, right = 0;
int len = INT_MAX, resL = -1;
for (; right < s.size(); ++right) {
char c = s[right];
if (umapt.find(c) != umapt.end())
umaps[c]++;
while (check() && left <= right) {
if (right - left + 1 < len) {
len = right - left + 1;
resL = left;
}
if (umapt.find(s[left]) != umapt.end()) {
umaps[s[left]]--;
}
left++;
}
}
return resL == -1 ? string() : s.substr(resL, len);
}
};