My code seems to get me the correct results so far. Except I've run into a problem. When I give it a string that is 8 characters long or longer, it doesn't seem to end. I'm already at 3 billion permutations and it's still running. When I tried the method with shorter strings, it terminated and gave correct output. I don't know what else to try and I'd appreciate any tips. Here is the Java code:
public static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
permutations++;
System.out.println(prefix);
System.out.println("Permutations: " + permutations);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i),
str.substring(0, i) + str.substring(i + 1));
}
}
}