Test cases fail to pass

Two test cases failed to pass and one is showing TLE
Help me with the approach and optimization .
Here is my code
"import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int k = scn.nextInt();
String s = scn.next();
int idx = 0;
int max = Integer.MIN_VALUE;

	for (int i = 0; i < s.length() - 1 && idx < s.length(); i++) {
		StringBuilder sb = new StringBuilder(s);
		int si = i;
		for (int o = si; o < i+k && o<sb.length(); o++) {
			if (sb.charAt(o) == 'a') {
				sb.setCharAt(o, 'b');
			} else {
				sb.setCharAt(o, 'a');
			}
		}

		for (int z = 0; z < sb.length() - 1; z++) {
			if (sb.charAt(z) != sb.charAt(z + 1)) {
				sb.insert(z + 1, "*");
				z++;
			}

		}
		

		max = Math.max(max, maxlength(sb.toString()));

	}
	System.out.println(max);

}
private static int maxlength(String sb) {
	// TODO Auto-generated method stub
	int max = Integer.MIN_VALUE;
	StringBuilder ss = new StringBuilder();
	for (int i = 0; i < sb.length(); i++) {
		if (sb.charAt(i) == '*') {
			StringBuilder ans = new StringBuilder();
			ss = ans;
		}
		if (sb.charAt(i) != '*') {
			ss.append(sb.charAt(i));
		}
		max = Math.max(max, ss.length());

	}
	return max;

}

}"

your approach is incorrect. you are making k continuous changes, so you are handling the cases where k continuous changes are required to make largest perfect string. what if k continuous changes can’t give the answer, for eg. in cases like “aaabaaabaaaa” and k=2, you can get whole string as perfect but your approach can never get this solution.
So think about right approach.
Hint: the approach is similar to classical dynamic programming problem "subset sum"with a restriction of maximum size of subset can be k.

Thanks