Maam is there any probelm if we solve the above question using StringBuilders.
public static void main(String[] args) {
System.out.println(permutationString("ab"));
}
public static ArrayList permutationString(String str) {
if(str.length()==0) {
ArrayList baseList = new ArrayList<>();
baseList.add(" ");
return baseList;
}
char cc =str.charAt(0);
String ros = str.substring(1);
ArrayList<String> myResult = new ArrayList<>();
ArrayList<String> recResult = permutationString(ros);
for(int i=0;i<recResult.size();i++) {
for(int j=0; j<=ros.length();j++) {
StringBuilder sb = new StringBuilder(recResult.get(i));// inserting strinBuilder to further insert
// if we transfer sb to above for then value will change
StringBuilder ans = sb.insert(j, cc);
String val = ans.toString(); // converting StringBuilder to string
myResult.add(val);
}
}
return myResult;
}
}