-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliding_window_substring_problem.cpp
More file actions
52 lines (45 loc) · 1.24 KB
/
Copy pathSliding_window_substring_problem.cpp
File metadata and controls
52 lines (45 loc) · 1.24 KB
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;
#define MAX 256
void minLengthWindow(string input , string pattern)
{
int shouldFind[MAX] = {0,} , hasfound[MAX] = {0,};
int j=0 ,cnt=0, start=0 ,finish, minwindow = INT_MAX;
int charlen = pattern.length() , iplen = input.length();
for(int i=0 ; i<charlen ; i++)
shouldFind[pattern[i]] += 1;
finish = iplen;
for(int i=0 ; i<iplen ; i++)
{
if(!shouldFind[input[i]])
continue;
hasfound[input[i]] += 1;
if(shouldFind[input[i]] >= hasfound[input[i]])
cnt++;
if(cnt == charlen)
{
while(shouldFind[input[j]] == 0 || hasfound[input[j]] > shouldFind[input[j]])
{
if(hasfound[input[j]] > shouldFind[input[j]])
hasfound[input[j]]--;
j++;
}
if(minwindow > (i-j+1))
{
minwindow = i- j + 1;
finish = i;
start = j;
}
}
}
cout<<"Desired window is : ";
for(int i=start ; i<=finish ; i++)
cout<<input[i];
}
int main()
{
string input = "abbacbaa";
string pattern = "aab";
minLengthWindow(input , pattern);
return 0;
}