forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_window_substr.cpp
More file actions
40 lines (40 loc) · 928 Bytes
/
minimum_window_substr.cpp
File metadata and controls
40 lines (40 loc) · 928 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
37
38
39
40
class Solution {
public:
string minWindow(string s, string t) {
if(s.size()<t.size())
return "";
unordered_map<char,int> mp;
for(auto i:t)
mp[i]++;
int n=mp.size();
int l=0,r=0;
int cnt=0;
int st=-1,sz=INT_MAX;
while(r<s.size())
{
auto k=mp.find(s[r]);
if(k!=mp.end())
{
if(--k->second==0)
cnt++;
}
if(cnt==n)
{
while(cnt==n&&l<=r)
{
auto j=mp.find(s[l]);
if(j!=mp.end()&&++j->second>0)
cnt--;
l++;
}
if(sz>r-l+2)
{
st=l-1;
sz=r-l+2;
}
}
r++;
}
return sz==INT_MAX?"":s.substr(st,sz);
}
};