-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0076.cpp
More file actions
38 lines (30 loc) · 796 Bytes
/
0076.cpp
File metadata and controls
38 lines (30 loc) · 796 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 (t.size() > s.size())
return "";
unordered_map<char, int> tFreq, window;
for (char c : t)
tFreq[c]++;
int have = 0, need = tFreq.size();
int l = 0, minLen = INT_MAX, minStart = 0;
for (int r = 0; r < s.size(); r++) {
char c = s[r];
window[c]++;
if (tFreq.count(c) && window[c] == tFreq[c])
have++;
while (have == need) {
if (r - l + 1 < minLen) {
minLen = r - l + 1;
minStart = l;
}
char lChar = s[l];
window[lChar]--;
if (tFreq.count(lChar) && window[lChar] < tFreq[lChar])
have--;
l++;
}
}
return minLen == INT_MAX ? "" : s.substr(minStart, minLen);
}
};