Skip to content

Commit

Permalink
#Sliding_window
Browse files Browse the repository at this point in the history
 Given string s, positive integer k, and a string char_value which is of length 26 and denotes whether the letter is special or not. special -> ‘1’  normal -> ‘0’. Find the maximum length of the substring which has at most k normal characters.  example of char_value 101011111111111111111111111   This denotes b and d are normal characters as 0 is in 2nd and 4th position.
  • Loading branch information
codewithshailendra authored Oct 15, 2019
1 parent adca3d5 commit 0d41583
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
string s; int k; cin>>s>>k; int n=s.length();

int start=0,end=0,zeroes=0, ans=INT_MIN,win_len=0; bool flag=false;

for(int i=0;i<n;i++)
{
while(i<n && zeroes<=k )
{
if(s[i]=='0')
zeroes++;

i++;
}

if(i==n && zeroes==k+1)
{cout<<n-1<<endl; flag=true; break;}
else if(zeroes==k+1)
{
win_len= i-start;
ans=max(ans,win_len);
}

if(s[start]=='0')
{
start++;
zeroes--;
}
else
{
while(start<n && s[start]!=0)
start++;

if(start==n)
{
cout<<ans<<endl; break;
}
else
{
start+=1;
zeroes--;
}
}





}

if(!flag)
cout<<ans<<endl;

return 0;


}

0 comments on commit 0d41583

Please sign in to comment.