forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03 string_window.cpp
75 lines (52 loc) · 1.32 KB
/
03 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<climits>
#include<string>
using namespace std;
string find_window(string s,string p){
//Array as a Freq Map or you can hashmap
int FP[256] = {0};
int FS[256] = {0};
for(int i=0;i<p.length();i++){
FP[ps[i]]++;
}
//Sliding Window Algorithm
int cnt = 0;
int start = 0; // left contraction
int start_idx = -1; //start_idx for best window
int min_so_far = INT_MAX; //large number
int window_size ;
for(int i=0 ; i < s.length(); i++){
//expand the window by including current character
char ch = s[i];
FS[ch]++;
//Count how many characters have been matched till now (string and pattern)
if(FP[ch]!=0 and FS[ch]<= FP[ch]){
cnt += 1;
}
//another thing
//if all characters of the pattern are found in the current window then you can start contracting
if(cnt==p.length()){
//start contracting from the left to remove unwanted characters
while(FP[s[start]]==0 or FS[s[start]] > FP[s[start]]){
FS[s[start]]--;
start++;
}
//note. the window size
window_size = i - start + 1;
if(window_size < min_so_far){
min_so_far = window_size;
start_idx = start;
}
}
}
if(start_idx==-1){
return "No window found";
}
return s.substr(start_idx,min_so_far);
}
int main(){
string s,p;
cin>>s>>p;
cout<<find_window(s,p)<<endl;
return 0;
}