1 回答
TA贡献1780条经验 获得超1个赞
<?php
// PHP program to print all
// permutations of a given string.
/**
* permutation function
* @param str string to
* calculate permutation for
* @param l starting index
* @param r end index
*/
function permute($str, $l, $r, $pspell_link)
{
if ($l == $r) {
if (pspell_check($pspell_link, $str)) {
echo $str. "\n";
};
}
else
{
for ($i = $l; $i <= $r; $i++)
{
$str = swap($str, $l, $i);
permute($str, $l + 1, $r, $pspell_link);
$str = swap($str, $l, $i);
}
}
}
/**
* Swap Characters at position
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
function swap($a, $i, $j)
{
$temp;
$charArray = str_split($a);
$temp = $charArray[$i] ;
$charArray[$i] = $charArray[$j];
$charArray[$j] = $temp;
return implode($charArray);
}
// Driver Code
$str = "ANAGRAM";
$n = strlen($str);
$pspell_link = pspell_new("en"); // pspell has to be enabled on your web server
permute($str, 0, $n - 1, $pspell_link);
// This code is contributed by mits.
?>
- 1 回答
- 0 关注
- 127 浏览
添加回答
举报