-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_Minimum_Window_Substring.cpp
58 lines (55 loc) · 1.25 KB
/
8_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
57
58
#include<bits/stdc++.h>
using namespace std;
string minWindow(string s, string t) {
if(t.length()>s.length())
return "";
if(s==t)
return s;
int i = 0, j = 0,start=0;
int count=0,strSize = INT_MAX;
map<char,int>mp;
for(auto it: t)
{
mp[it]++;
}
int map_size = mp.size();
while(j<s.length())
{
if(mp.find(s[j])!=mp.end())
{
mp[s[j]]--;
if(mp[s[j]]>=0)
{
count++;
}
}
while(count==t.length())
{
if(j-i+1 < strSize)
{
start = i;
strSize = j-i+1;
}
if(mp.find(s[i])!=mp.end())
{
mp[s[i]]++;
if(mp[s[i]]>0)
count--;
}
i++;
}
j++;
}
if(strSize == INT_MAX)
strSize = 0;
return s.substr(start,strSize);
}
int main()
{
string v;
cin>>v;
string k;
cin>>k;
string g = minWindow(v,k);
cout<<g;
}