public static void permutation(String str) {
permutation("", str);
}
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));
}
}
Reference: http://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string
本文介绍了一种使用递归算法来生成给定字符串的所有可能排列的方法,并提供了一个简洁的Java实现示例。对于输入字符串“abcdefgh”,该算法能高效地输出所有可能的组合。
1001

被折叠的 条评论
为什么被折叠?



