-
-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathPDKhan.cpp
More file actions
44 lines (34 loc) · 1.14 KB
/
PDKhan.cpp
File metadata and controls
44 lines (34 loc) · 1.14 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:
string minWindow(string s, string t) {
unordered_map<char, int> t_map;
unordered_map<char, int> window;
int need = 0, match = 0;
int min_len = INT_MAX;
int min_low = 0;
int l = 0;
for(char ch : t){
if(t_map[ch] == 0)
need++;
t_map[ch]++;
}
for(int h = 0; h < s.length(); h++){
window[s[h]]++;
if(window[s[h]] == t_map[s[h]])
match++;
while(need == match){
if(h - l + 1 < min_len){
min_len = h - l + 1;
min_low = l;
}
window[s[l]]--;
if(window[s[l]] < t_map[s[l]])
match--;
l++;
}
}
if(min_len == INT_MAX)
return "";
return s.substr(min_low, min_len);
}
};