SanketAndStrings

#include
#include
using namespace std;
int length(string s, int k)
{
int a_len = 0, b_len = 0;
int len = s.length();
for(int i = 0; i < len; i++)
{
if(s[i] == ‘a’)
a_len++;
else
b_len++;
}
if(a_len == b_len)
return 2 * a_len;
while(k–)
{
if(a_len == b_len)
return 2 * a_len;
else if(a_len < b_len)
{
a_len++;
b_len–;
}
else
{
a_len–;
b_len++;
}
}
if(a_len < b_len)
return 2* a_len;
return 2* b_len;

}

int main()
{
int n;
cin>>n;
string s;
cin>>s;
cout<<length(s, n);
return 0;
}
I am able to clear only one test case, please suggest some test cases and where I am doing wrong

Hello @Divya_321,

Always share codes using Online Coding Blocks IDE: https://ide.codingblocks.com/
Steps:

  1. Paste your code there.
  2. Save it there.
  3. Share the URL generated.

Now, coming back to you problem,
Example:
2
baaab
Expected Output:
5
Your Output:
4

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

please , tell me how it is 4 as the question says the string contains equal number of character means no. of a == no. of b
so how the output is 5 please explain

Hey @Divya_321,

Sanket describes perfectness of a string as the maximum length substring of equal characters.

In the above statement equal is a reference to same i.e. either all a or all b.
So, in the given sample testcase, answer is 4 because after performing 2 replacements:
There are two possible perfectness: aaaa or bbbb
which has length : 4(max)

Hope, it is clear now.