Delete comment from: Java67
Well I answered it so I might as well post it here..
(Might be some better String/StringBuilder API functions than the ones I used)..
public static List permutations(String str) {
List res = new ArrayList();
if (str.length() < 2) {
res.add(str);
return res;
}
List currRes = null;
for (int i=0; i<str.length(); i++) {
StringBuilder newStr = new StringBuilder(str);
char tmpchar = newStr.charAt(i);
newStr.setCharAt(i, newStr.charAt(0));
newStr.setCharAt(0,tmpchar);
currRes = permutations(newStr.substring(1));
for (String s : currRes) {
res.add(newStr.charAt(0) + s);
}
}
return res;
}
Oct 18, 2014, 1:02:36 PM
Posted to Top 10 Java Programs, Assignments, and Coding Interview Questions Answers