Hi all,
I have a PHP script that takes a string and lists all its permutations, but ONLY based on its length. So for instance, let's just say we had the word "racecar". Yes, it would list all permutations of the 7-character word "racecar" such as "acrerac", etc. but NOT words like "race" or "car" that have a smaller length. Here is my code:
function check($word)
{
if (strlen($word) < 2)
{
return array($word);
}
$permutations = array();
$tail = substr($word, 1);
foreach (check($tail) as $permutation)
{
$length = strlen($permutation);
for ($i = 0; $i <= $length; $i++)
{
$permutations[] = substr($permutation, 0, $i) . $word[0] . substr($permutation, $i);
}
}
return $permutations;
}
Thanks in advance!