SAnket & Strings DOUBT


Please check what wrong I am doing in this approach.

@gsinha783
You are running a loop with variable i and using l in your code.
Instead you should run the loop with l only
for(int l=0;l<n;l++){

Also , your value of k becomes 0 after the first iteration only and you continue with that value.
You should save the value of k in another variable say x
int x = k;
This is before the for loop.
After this , you should put the value of k back to its original so you can work with it again
int x = k;
for(int l=0;l<n;l++){
r = l ;
k = x ;
while (r < n) {

Make these changes.
Note that your code will still fail one testcase after these changes. This is the corner case when the entire string is of same characters.
Input :
6
aaaaa

Expected Output :
5

Your Output :
6

Try to figure out your way around this corner case and let me know if you need any help with it.

I have done the suggested changes but its not working still

@gsinha783
Share your new code.

#include<bits/stdc++.h>
using namespace std;

void sanket_string(string s,int n,int k){
int r=0; vector res;
int x=k; int flag=0;
for(int l=0;l<n;l++){
r=l;k=x;
if(k==x){
flag=1;
break;
}
while(r<n){
if(s[l]==s[r]){
r++;
}
if(s[l]!=s[r] and k>0){
k–;
r++;
}
if(s[l]!=s[r] and k<=0){
break;
}

       }
        int ans = r-l;
        res.push_back(ans);
   }
    if(flag==1){
        cout<<n;
        return;
    }
       auto itr = max_element(res.begin(),res.end());
       cout<<*itr<<endl;

}

int main() {
int k; cin>>k;
string a; cin>>a;
int n = a.length();
sanket_string(a,n,k);
return 0;
}

@gsinha783
Your code contradicts itself.
You assign k as x in statement k=x
In the next statement you go on check whether k==x ( which you just made so in the previous statement ) and then if it is ( which it always will be since you made it so ) , you break the loop.
Your code does not even do a single iteration.

#include<bits/stdc++.h>
using namespace std;

void sanket_string(string s,int n,int k){
int r=0; vector res;
int x=k; int flag=0;
for(int l=0;l<n;l++){
if(k==x){
flag=1;
break;
}
r=l;k=x;

       while(r<n){
           if(s[l]==s[r]){
               r++;
           }
           if(s[l]!=s[r] and k>0){
               k--;
               r++;
           }
           if(s[l]!=s[r] and k<=0){
               break;
           }

       }
        int ans = r-l;
        res.push_back(ans);
   }
    if(flag==1){
        cout<<n;
        return;
    }
       auto itr = max_element(res.begin(),res.end());
       cout<<*itr<<endl;

}

int main() {
int k; cin>>k;
string a; cin>>a;
int n = a.length();
sanket_string(a,n,k);
return 0;
}

@gsinha783
Still no difference as when we begin the for loop , the value of x and k are still the same since we assigned x = k only. The condition is still hit and none of your code in the loop is executed.

Can you please help me to correct it?

@gsinha783
Update your original code ( the first code you shared ) with the changes I suggested. That will solve most of your problems. Now the only thing that’s left is the corner case when all characters are same like
Input :
6
aaaaa

Expected Output :
5

These are the only cases your code will be missing out. This will be so when r-l generates a value larger than s.size(). You can put this comparison in the loop.
if(r-l > s.size() )
In such a case , do not push r-l into the res vector rather push s.size() as your answer cannot be greater than s.size() . Also in such a case you can put a break statement as there is no need to compute any further answers as you have already got the maximum possible answer.

If this condition is not hit , simply push r-l into the res vector as you already have been and proceed the same way.

By the way I have run my code for the case when all the characters are same and it is working. (the first code which I shared)

@gsinha783
Your original code was only working for those testcases and failing most others. So that was the entire problem.
Also since you have saved your new code over the same URL code , I can no longer access your original code.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.