i have passed only one test cases out of four where is the problem in my code… please correct it
Recursion dictionary order larger
@Amre-8800 Hi bro, for comparing strings use compareTo always, it compares the strings lexicographically.
Here I have corrected your code.
import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
String str = sc.next();
ArrayList<String> ans = getPermutation(str);
Collections.sort(ans);
for (int i = 0; i < ans.size(); i++) {
//Use compareTo for comparing strings
if(ans.get(i).compareTo(str) > 0)
System.out.println(ans.get(i));
}
}
public static ArrayList<String> getPermutation(String str) {
if (str.length() == 0) {
ArrayList<String> br = new ArrayList<String>();
br.add("");
return br;
}
char cc = str.charAt(0);
String ros = str.substring(1);
ArrayList<String> mr = new ArrayList<String>();
ArrayList<String> rr = getPermutation(ros);
for (String rrs : rr) {
for (int i = 0; i <= rrs.length(); i++) {
String element = rrs.substring(0, i) + cc + rrs.substring(i);
mr.add(element);
}
}
return mr;
}
}
If your doubt is solved, resolve the doubt and rate it full, else ask your query.