-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0076.cpp
More file actions
24 lines (23 loc) · 732 Bytes
/
Copy path0076.cpp
File metadata and controls
24 lines (23 loc) · 732 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
class Solution {
public:
string minWindow(string s, string t) {
vector<int> remaining(128, 0);
for (char c : t)
remaining[c]++;
int required = t.size();
int min = INT_MAX, start = 0, left = 0, i = 0;
while (i <= s.size() && start < s.size()) {
if (required) {
if (i == s.size()) break;
if (--remaining[s[i++]] >= 0) required--;
} else {
if (i - start < min) {
min = i - start;
left = start;
}
if (++remaining[s[start++]] > 0) required++;
}
}
return min == INT_MAX ? "" : s.substr(left, min);
}
};