-1

I am looking for an algorithm that will provide me with all possible outcomes of same length and no repeat.

INPUT
---------------
abc


OUTPUT
---------------
abc
acb
bac
bca
cab
cba
Soumik Sur
  • 167
  • 8

1 Answers1

1

Take a look here man: 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.
Community
  • 1
  • 1
zeflex
  • 1,487
  • 1
  • 14
  • 29