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;
}
}"