All 3 testcases showing wrong answer

Here is my code :

import java.util.*;
public class Main {

public static int for_char (String A, char a) {
	int maxlen = 1;
	int count = 0;
	int left = 0;
	int right = 0;
	
	while (right < A.length()) {
		if (A.charAt(right) != a) {
			count++;
		}
		
		while (count > 2) {
			if (A.charAt(left) != a) {
				count--;
			}
			left++;
		}
		
		maxlen = Math.max(maxlen, right - left + 1);
		right++;
	}
	return maxlen;
}

public static void main(String args[]) {
	Scanner s = new Scanner(System.in);
	int n = s.nextInt();
	String ss = s.next();
	int max = 1;
	max = Math.max(max, for_char(ss, 'a'));
	max = Math.max(max, for_char(ss, 'b'));
	System.out.println(max);		
}

}

Please tell me where am I going wrong.

@gagan123malik123,
Input:
3
abababab
Correct output:
7
Your Output:
5
When counting say char = ‘a’ :

  1. Maintain a count variable which counts the number of swaps made or the number of 'b’s in our A window.
  2. If you encounter a ‘b’ char at s[ j ] position , increment the count variable. Count should never exceed k .
  3. Take the size of the window at every point using length = j - i + 1;
  4. Compute the max size window this way and do the same for ‘b’ as well.
  5. Compare the maximum size window of ‘a’ and ‘b’

@gagan123malik123,
You are not using the input int n anywhere in your code. It is basically the the maximum number of characters you can change in the string

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.