Kartik Bhaiya and Strings

Hello,
I have written the follwing code for this question and I am not able to understand why 2 of the testcases are failing for this code as I have tried the dry run and it should work fine as per me, so can you kindly guide me where am I making mistake ?

#include
#include
#include
using namespace std;
int largest(int var_1, int var_2)
{
if(var_1>var_2)
{
return var_1;
}
else
{
return var_2;
}
/else{
return var_1;
}
/
}
int count_b_func(int k, char temp[])
{
int len = strlen(temp);
int count_b = 0;
int a = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==β€˜b’)
{
count_b++;
}
if(temp[i]==β€˜a’)
{
a++;
}

}

if(k>a)
{
	count_b = count_b+a;
}
else if(k<=a)
{
	count_b = count_b+k;

}

return count_b;

}

int count_a_func(int k, char temp[])
{
int len = strlen(temp);
int count_a = 0;
int b = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==β€˜a’)
{
count_a++;
}
if(temp[i]==β€˜b’)
{
b++;
}

}

if(k>b)
{
	count_a = count_a+b;
}
else if(k<=b)
{
	count_a = count_a+k;

}

return count_a;

}

int main() {
int len_a =0;
int len_b =0;
int k;
cin>>k;
cin.get();
char a[1000];
char temp[1000];
cin.getline(a,1000);
int len = strlen(a);
int j;
int a_num;
int b_num;
int perf;
//1.getting substrings
for(int i=0;i<=len-1;i++)
{
int x=0;
for(j =i;j<=len-1;j++)
{
temp[x++] = a[j]; //substr stored in temp arr.

	}
	temp[j] = '\0';
	//cout<<temp<<endl;
	a_num = count_a_func(k,temp);
	//cout<<"No of a "<<a_num<<endl;
	b_num = count_b_func(k,temp);
	//cout<<"No of b "<<b_num<<endl;
	len_a = largest(a_num,len_a);
	len_b = largest(b_num,len_b);


}
perf = largest(len_a,len_b);
cout<<perf;

return 0;

}

hi @agganushka_9eb7cecc88bfac61

Approach - Two pointer approach

You can solve this problem in O(n) time using the two pointer approach.

  • Make two variabes , say i and j .
  • i defines the beginning of a window and j defines its end.
  • Start i from 0 and j from k.
  • Let’s talk about the singular case when we are considering the max window for only 'a’s and consider only the swapping of b-> a. If we are able to get the answer for max window of consecutive 'a’s , we can simply implement the same algo for the max β€˜b’ window as well.
  • So we started i from 0 and j from k.
  • Move j ahead freely as long as there are β€˜a’ characters at s[ j ] position.
  • Maintain a count variable which counts the number of swaps made or the number of 'b’s in our A window.
  • If you encounter a β€˜b’ char at s[ j ] position , increment the count variable. Count should never exceed k .
  • Take the size of the window at every point using length = j - i + 1;
  • Compute the max size window this way and do the same for β€˜b’ as well.
  • Output the maximum size window of β€˜a’ and β€˜b’.

refer this 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.