-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path076_Minimum_Window_Substring.cpp
56 lines (55 loc) · 1.37 KB
/
076_Minimum_Window_Substring.cpp
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
#include<climits>
#include<map>
#include<string>
#include<iostream>
using namespace std;
class Solution
{
public:
string minWindow(string s, string t)
{
map<char,int> t_map;
for(char c: t)
t_map[c]++;
int counter = t_map.size();
int begin = 0, end = 0, len = INT_MAX;
string res;
while(end < s.length())
{
char end_char = s[end];
if(t_map.count(end_char) == 1)
{
t_map[end_char]--;
if(t_map[end_char] == 0)
counter--;
}
end++;
while(counter == 0)
{
cout<<"\t"<<s.substr(begin, end)<<endl;
if(end-begin < len)
{
res = s.substr(begin, end-begin);
len = end -begin;
}
char begin_char = s[begin];
if(t_map.count(begin_char) == 1)
{
t_map[begin_char]++;
if(t_map[begin_char] > 0)
counter++;
}
begin++;
}
}
return res;
}
};
int main(int argc, char const *argv[])
{
string s = "ADOBECODEBANC", t = "ABC";
//string s = "bac", t = "a";
Solution solu;
cout<<solu.minWindow(s,t);
return 0;
}