/* All Permutations of a String */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Complexity O(n*n!) */
class Ideone
{
public static ArrayList
<String
> strPerm
(String str, ArrayList
<String
> list
){ int len = str.length();
if(len==1){
list.add(str);
return list;
}
list = strPerm(str.substring(0,len-1),list);
int ls = list.size();
char ap = str.charAt(len-1);
for(int i=0;i<ls;i++){
int tl = temp.length();
for(int j=0;j<=tl;j++){
list.add(temp.substring(0,j)+ap+temp.substring(j,tl));
}
}
while(true){
if(temp.length()<len)
list.remove(temp);
else
break;
}
return list;
}
{
ArrayList<String> list = new ArrayList<>();
list = strPerm(str,list);
System.
out.
println("Total Permutations : "+list.
size()); for(int i=0;i<list.size();i++)
System.
out.
println(list.
get(i
));
}
}
LyogQWxsIFBlcm11dGF0aW9ucyBvZiBhIFN0cmluZyAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBDb21wbGV4aXR5IE8obipuISkgKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgQXJyYXlMaXN0PFN0cmluZz4gc3RyUGVybShTdHJpbmcgc3RyLCBBcnJheUxpc3Q8U3RyaW5nPiBsaXN0KXsKCQlpbnQgbGVuID0gc3RyLmxlbmd0aCgpOwoJCWlmKGxlbj09MSl7CgkJCWxpc3QuYWRkKHN0cik7CgkJCXJldHVybiBsaXN0OwoJCX0KCQkKCQlsaXN0ID0gc3RyUGVybShzdHIuc3Vic3RyaW5nKDAsbGVuLTEpLGxpc3QpOwoJCWludCBscyA9IGxpc3Quc2l6ZSgpOwoJCWNoYXIgYXAgPSBzdHIuY2hhckF0KGxlbi0xKTsKCQlmb3IoaW50IGk9MDtpPGxzO2krKyl7CgkJCVN0cmluZyB0ZW1wID0gbGlzdC5nZXQoaSk7CgkJCWludCB0bCA9IHRlbXAubGVuZ3RoKCk7CgkJCWZvcihpbnQgaj0wO2o8PXRsO2orKyl7CgkJCQlsaX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