-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
adca3d5
commit 0d41583
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...SHIP EXAM QUESTIONS/PUBLICIS SAPIENT/maximum length binary substring with atmost k zeroes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
} |