-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathString Window.cpp
61 lines (54 loc) Β· 1.55 KB
/
String Window.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
59
60
61
/* Find window with minimum length which contains all the characters of second string
* Approach: Fisrt find window which contains all characters of string 2 and then try to minimize that window
* https://www.geeksforgeeks.org/find-the-smallest-window-in-a-string-containing-all-characters-of-another-string/
*/
#include<bits/stdc++.h>
using namespace std;
string minimumWindowString(string a, string b)
{
if(a.length()<b.length())
{
return "No string";
}
unordered_map<char,int> str,pat;
for(int j=0;j<b.length();j++)
pat[b[j]]++;
int start=0,start_index=-1;
int min_len=INT_MAX;
int cnt=0;
for(int j=0;j<a.length();j++)
{
str[a[j]]++;
if(pat.find(a[j])!=pat.end() && str[a[j]]<=pat[a[j]])
{
cnt++;
}
if(cnt==b.length()) ///which means we have found one such window so now we need to minimize it
{
while(pat.find(a[start])==pat.end() || str[a[start]]>pat[a[start]])
{
if(str[a[start]]>pat[a[start]])
str[a[start]]--;
start++;
}
if((j-start+1)<min_len)
{
min_len=j-start+1;
start_index=start;
}
}
}
if(start_index!=-1)
return a.substr(start_index,min_len);
else
return "No string";
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
string a,b;
getline(cin,a);
getline(cin,b);
cout<<minimumWindowString(a,b);
}