Combination of letters using php

by Mohan 2013-10-05 16:46:38




<h1><font color=#00004D>Combination of letters using php</font></h1>


// 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 = "sad";
permute($str,0,strlen($str)); // call the function.



Will display the combination as


sad sda asd ads das dsa


776
like
0
dislike
0
mail
flag

You must LOGIN to add comments