-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsmallest_window_in_string_containing_another_pattern.cpp
52 lines (51 loc) · 1.29 KB
/
smallest_window_in_string_containing_another_pattern.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
#include<bits/stdc++.h>
using namespace std;
string findWindow(string str, string pat){
if(pat.size() > str.size())
return "-1";
int hashPat[256]={0};
int hashStr[256]={0};
for(int i=0; i<pat.size(); i++){
hashPat[pat[i]]++;
}
int count = 0;
int startMin = -1;
int start = 0;
int minWin = INT_MAX;
for(int i=0; i<str.size(); i++){
if(hashPat[str[i]]!=0){ // useful char
if(hashPat[str[i]] > hashStr[str[i]]){
count++;
}
hashStr[str[i]]++;
if(count == pat.size()){
while(hashPat[str[start]] == 0 || hashPat[str[start]]<hashStr[str[start]]){
if(hashPat[str[start]] != 0){
hashStr[str[start]]--;
}
start++;
}
if(minWin > i-start+1){
startMin = start;
minWin = i-start+1;
}
}
}
}
if(startMin!=-1){
return str.substr(startMin, minWin);
}
return "-1";
}
int main() {
int T;
cin >> T;
for(int i=0; i<T; i++){
string pat;
string str;
cin >> str;
cin >> pat;
cout << findWindow(str, pat) << endl;
}
return 0;
}