TLE in case 3 and rest cases are correct
TLE in case 3 and rest cases are correct
@jatin111 Bro don’t use inefficient sorting algorithm, instead just use Arrays.sort(name), it will pass all!
Arrays.sort func does not allow char array
@jatin111 First of all You are making Array of strings not char, recheck ya code.
Here is your corrected code, as I suggested.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
ArrayList res = permutations(str, str.length());
int count = 1;
String t;
String name[] = res.toArray(new String[res.size()]);
Arrays.sort(name);
for (int i = 0; i < name.length; i++) {
if (name[i].equals(str)) {
break;
} else {
System.out.println(name[i]);
}
}
}
public static ArrayList<String> permutations(String str, int len) {
if (str.length() == 0) {
ArrayList<String> base = new ArrayList<>();
base.add("");
return base;
}
char ch = str.charAt(0);
String ros = str.substring(1);
ArrayList<String> rr = new ArrayList<>();
ArrayList<String> mr = new ArrayList<>();
rr = permutations(ros, len - 1);
for (int i = 0; i < rr.size(); i++) {
String s = rr.get(i);
for (int j = 0; j <= s.length(); j++)
mr.add(s.substring(0, j) + ch + s.substring(j, s.length()));
}
return mr;
}
}
See this i have clearly mentioned name as array, and name stores strings!