Move All X at End

import java.util.*;
public class Main {
public static String MoveX(String s, int n, String ans) {

  if(s.length()==n){
        return ans;
    }
    if (s.length()==n-1){
        String sol = MoveX(s, n+1, ans + s.charAt(n));
        return sol;
    }
    if(s.substring(n)=="x"){
        String sol = MoveX(s, n+1, ans) + "x";
        return sol;
    }
    else{
        String sol = MoveX(s, n+1, ans + s.charAt(n));
        return sol;
    }

}
public static void main(String args[]) {
   
    System.out.println(MoveX("axbc", 0, ""));
}

}

I wrote the above code. But it keeps returning the same string.

hi @SrishtiSehgal
you have to check the the character not the substring in your function.
if(s.charAt(n)==‘x’){
String sol = MoveX(s, n+1, ans) + “x”;
return sol;
}

@SrishtiSehgal
import java.util.*;

public class Main {
public static String MoveX(String s, int n, String ans) {

if(s.length()==n){
return ans;
}
if(s.charAt(n)==‘x’){
String sol = MoveX(s, n+1, ans) + “x”;
return sol;
}
else{
String sol = MoveX(s, n+1, ans + s.charAt(n));
return sol;
}

}
public static void main(String args[]) {

System.out.println(MoveX("axbxc", 0, ""));

}
}

if have any dout you can ask otherwise mark your doubt as solved and rate me as well.