17. Permutations
Print out all the permutations of a given string of letters.
Description: Print out all the permutations of a given string of letters.
Code
package asu.permutations;
public class Permutations {
public static void main(String[] args) {
permutation("", "abcdef");
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
}
}Output
Last updated
Was this helpful?