-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.minimum-window-substring.cpp
More file actions
64 lines (54 loc) · 1.92 KB
/
76.minimum-window-substring.cpp
File metadata and controls
64 lines (54 loc) · 1.92 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
string minWindow(string s, string t) {
int left = 0;
int right = 0;
int res_len = INT_MAX;
string ans = "";
map<char, int> char_count;
map<char, int> window_count;
fillMap(char_count, t);
int s_len = s.length();
while(right < s_len){
if(window_count.find(s[right]) == window_count.end())
window_count[s[right]] = 1;
else
window_count[s[right]]++;
// cout<<s.substr(left, right-left+1)<<"\t"<<left<<"\t"<<right<<endl;
while(satisfies(char_count, window_count) && left <= right){
if(right-left < res_len){
res_len = right - left;
ans = s.substr(left, right-left+1);
}
window_count[s[left]]--;
left++;
}
right++;
}
return ans;
}
bool satisfies(map<char, int>& char_count, map<char, int>& window_count){
// cout<<"--------------------"<<endl;
for(auto ch : char_count){
if(ch.second > window_count[ch.first]){
// cout<<"Satisfies : "<<false<<endl;
return false;
}
// cout<<ch.first<<"\t"<<ch.second<<"\t"<<window_count[ch.first]<<endl;
}
// cout<<"Satisfies : "<<true<<endl;
return true;
}
void fillMap(map<char, int>& char_count, string t){
// cout<<"Initial map : "<<endl;
for(int i=0; i<t.length(); i++){
if(char_count.find(t[i]) == char_count.end())
char_count[t[i]] = 0;
char_count[t[i]]++;
}
// for(auto ch : char_count){
// cout<<ch.first<<"\t"<<ch.second<<"\t"<<endl;
// }
// cout<<"--------------"<<endl;
}
};