forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathys-han00.cpp
More file actions
36 lines (30 loc) · 927 Bytes
/
ys-han00.cpp
File metadata and controls
36 lines (30 loc) · 927 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
class Solution {
public:
string minWindow(string s, string t) {
int l = 0, substr = 0, min_l = 0, min_r = s.size();
map<char, int> cnt;
for(char c : t)
cnt[c]++;
for(int r = 0; r < s.size(); r++) {
if(cnt.find(s[r]) != cnt.end()) {
if(cnt[s[r]] > 0)
substr++;
cnt[s[r]]--;
}
while(substr == t.size()) {
if(r - l < min_r - min_l) {
min_l = l;
min_r = r;
}
if(cnt.find(s[l]) != cnt.end()) {
cnt[s[l]]++;
if(cnt[s[l]] > 0)
substr--;
}
l++;
}
}
string ans = (min_r < s.size() ? s.substr(min_l, min_r - min_l + 1) : "");
return ans;
}
};