This is very simple problem: 266B — Queue at the School. But somehow the codefoces compiler tells that i got wrong answer (BGGBG), although in my compiler it is right(GBGGB).
import java.util.Scanner;
public class Codeforces {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int t = scn.nextInt();
String str = scn.next();
char[] chr = str.toCharArray();
boolean chng = true;
for(int ti = 1; ti <= t && chng; ti++){
chng = false;
for(int ni = 0; ni < n - 1; ni++){
if(chr[ni + 1] == 'g' && chr[ni] == 'b'){
chr[ni + 1] = 'b';
chr[ni] = 'g';
ni++;
chng = true;
}
}
}
for(int i = 0; i < n; i++){
System.out.print(chr[i]);
}
}}




