SanketAndStrings (the below code is giving error in Test case 0) I have used nested for loop for solving please explain the optimized approach

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int k;
	cin>>k;
	string str;
	cin>>str;
	int count=1;
	int maxi=1;
	int ok=k;
	int maximum=1;
	for(int j=1;j<str.length();j++)
	{
		char prev=str[j-1];
		count=1;
		k=ok;
		maximum=max(maximum,maxi);
	for(int i=j;i<str.length();i++)
	{
		if(str[i]==prev)
		{
			count++;
	}
		else
		{
			if(k>0)
			{
			count++;
			k--;
			continue;
			}
			prev=str[i];
			if(count>maxi){
				maxi=count;
			}
			count=1;
		}
		if(count>maxi)
		{
			maxi=count;
		}
	}
	}
	cout<<maximum<<endl;
}

@praritv1
This problem can be solved with help of two pointers. Let the first pointer is l and the second pointer is r. Then for every position l we will move right end r until on the substring si.si + 1… sr it is possible to make no more than k swaps to make this substring beautiful. Then we need to update the answer with length of this substring and move l to the right.

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.