没有重复,准确长度,所有可能的字符串组合在PHP
问题描述:
我正在寻找一种算法,将提供我所有可能的结果相同的长度和不重复。没有重复,准确长度,所有可能的字符串组合在PHP
INPUT
---------------
abc
OUTPUT
---------------
abc
acb
bac
bca
cab
cba
答
看看这里的男人:How to generate all permutations of a string in PHP?
// function to generate and print all N! permutations of $str. (N = strlen($str)).
function permute($str,$i,$n) {
if ($i == $n)
print "$str\n";
else {
for ($j = $i; $j < $n; $j++) {
swap($str,$i,$j);
permute($str, $i+1, $n);
swap($str,$i,$j); // backtrack.
}
}
}
// function to swap the char at pos $i and $j of $str.
function swap(&$str,$i,$j) {
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
$str = "hey";
permute($str,0,strlen($str)); // call the function.
+0
它必须被引用到此:http://stackoverflow.com/a/2617080/2458892 – ajdeguzman
亚我已经试过..但它不是的确切长度..得到了一些prevous后帮助..但没有apporpriate .. –
展我们尝试了什么。 – zeflex