I'm trying to permute a String and copy all the permutations into an ArrayList
. But I'm getting an infinite loop, and I fail to see where. Any help?
private static List<String> permute(String str) {
List<String> permutations = new ArrayList<String>();
return permuteHelper("", str, permutations);
}
private static List<String> permuteHelper(String prefix, String str, List<String> permutations) {
int length = str.length();
if (length == 0) {
permutations.add(prefix);
} else {
for (int i = 0; i < length; i++) {
permuteHelper(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, length), permutations);
}
}
return permutations;
}