Not able to understand the code ! Please provide a solution or optimize my given code

my test case 1 and 2 are not running



#include
#include<string.h>
using namespace std;

int main()
{
int k,c1=0,c2=0;
cin>>k;

cin.ignore();

string s;
getline(cin,s);
    
char ch='a';

for(int i=0; i<k; i++)
{   
    for(int j=0; j<s.length(); j++)
    {
        if(s[j]!=ch)
        {
            s[j]=ch;
            break;
        }
    }
               
}
for(int i=0 ; i< s.length(); i++ )
{
   if(s[i]==ch)
   {
       c1++;
   }
   else
   c2++;
}

if(c1>c2)
{
cout<<c1;
}
else
cout<<c2;

return 0;

}

Hello @sharmaharsh9944,

In your code you are only looking for the strings of ‘a’.
You have to also check for ‘b’.
Example:
4
baabb
Expexted Output:
5 (for bbbbb)
//Replaced two ‘a’ with ‘b’
Your Output:
4 (for aaaab)

Hope, this would help.
Give a like if you are satisfied.

no i m getting right answer … please provide me a code in which all test cases are passing!!!

Hey @sharmaharsh9944,

  1. No, your code is not giving right answer for the test-case i have specified.

  2. If you just want a code:
    https://ide.codingblocks.com/s/146198

Hope, this would help.
Give a like if you are satisfied.

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.