-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0076-minimum-window-substring.cpp
45 lines (44 loc) · 1.11 KB
/
0076-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
class Solution {
public:
string minWindow(string s, string t) {
if(t.size()>s.size())
return "";
unordered_map<char,int>srr,trr;
for(auto x:t)trr[x]++;
int prev=0;
string result;
for(int i=0;i<s.size();i++)
{
if(trr.count(s[i]))
{
srr[s[i]]++;
}
if(isMatched(srr,trr))
{
do
{
if(result.size()>i-prev+1||result=="")
result=s.substr(prev,i-prev+1);
srr[s[prev]]--;
if(srr[s[prev]]==0)
{
srr.erase(s[prev]);
}
prev++;
}while(isMatched(srr,trr));
}
}
return result;
}
bool isMatched(unordered_map<char,int>&srr,unordered_map<char,int>&trr)
{
for(auto x:trr)
{
if(!srr.count(x.first)||srr[x.first]<x.second)
{
return false;
}
}
return true;
}
};