Saket And string

Sanket has a string consisting of only ‘a’ and ‘b’ as the characters. Sanket describes perfectness of a string as the maximum length substring of equal characters. Sanket is given a number k which denotes the maximum number of characters he can change. Find the maximum perfectness he can generate by changing no more than k characters.

Input Format:
The first line contains an integer denoting the value of K. The next line contains a string having only ‘a’ and ‘b’ as the characters.

Constraints:
2 ≤ N ≤ 10^5

Output Format
A single integer denoting the maximum perfectness achievable.

Sample Input
2
abba
Sample Output
4

C++
Download Testcases
Unlock Editorial
Submit Code
Custom Input

Run Code
Output :

int answer(string s,int n,int k){

int count1 = 0;
int count2 = 0;	

for(int i = 0; i < n; ++i){
	
	if(s[i] == 'a'){
		count1++;
	}

	if(s[i] == 'b'){
		count2++;
	}

}

int count  = max(count1,count2);
return count+k ;

}

int main(){

int k;
cin >> k ;

string s;
cin >> s;

int n = s.length();

cout << answer(s,n,k);

}

I am not able to pass the testcases
Sombody please clear my doubt