forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0076.cpp
More file actions
38 lines (31 loc) · 987 Bytes
/
0076.cpp
File metadata and controls
38 lines (31 loc) · 987 Bytes
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
class Solution {
public:
string minWindow(string s, string t) {
if (s.empty() || t.empty()) return "";
unordered_map<char, int> map;
for (const char& c : t) map[c]++;
int required = map.size();
int l = 0;
int r = 0;
int bestLeft = 0;
int bestRight = 0;
int windowLen = s.length() + 1;
for (int r = 0; r < s.length(); r++) {
if (map.count(s[r]))
if (--map[s[r]] == 0)
required--;
while (required == 0 && l <= r) {
if (r - l + 1 < windowLen) {
windowLen = r - l + 1;
bestLeft = l;
bestRight = r;
}
if (map.count(s[l]))
if (++map[s[l]] > 0)
required++;
l++;
}
}
return windowLen == s.length() + 1 ? "" : s.substr(bestLeft, windowLen);
}
};