i have used string builder but sill the code is not working —
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int k = cin.nextInt();
String str1 = cin.nextLine();
StringBuilder str = new StringBuilder(str);
int counta = 0, countb =0;
for(int i = 0;i<str.capacity();i++)
{
if(str.charAt(i) == ‘a’)
{ counta++; }
else
{ countb++; }
} int count = 0;
char ch1 = ‘a’, ch2 = ‘b’;
if(counta>countb){
while(count<=k){
for(int i = 0;i<str.capacity();i++)
{
if(str.charAt(i) == ‘b’)
{
str.charAt(i) = ch1;
count++;
}
}
}
}else
{
while(count<=k)
{
for(int i = 0;i<str.capacity();i++)
{
if(str.charAt(i) == ‘a’)
{
str.charAt(i) = ch2; count++;
}
}
}
}
System.out.println(str);
}
}