-3

Possible Duplicate:
How to generate all permutations of a string in PHP?

I want to make a script in php that will take this input:

12a

And output a result like so:

1, 2, a, 12, 1a, 21, 2a, a1, a2, 12a, 1a2, 21a, 2a1.

I did some research but I cannot find any script that will do this.

Community
  • 1
  • 1
lexvdpoel
  • 39
  • 4
  • And the question is "where can I find such a script"? Whats the problem you've faced? – KingCrunch Apr 27 '12 at 21:46
  • that i cant find it and ask help isnt that obviose? [bad englisch :P] – lexvdpoel Apr 27 '12 at 21:48
  • 5
    The result you give doesn't follow any obvious pattern. At first it looks like it's giving all possible substrings of rearrangements of the input string, but it is missing `a12` and `a21`. – Hammerite Apr 27 '12 at 21:51
  • 2
    It should be releated to algorithms. So possible dublicate of [Finding all the subsets of a set](http://stackoverflow.com/questions/728972/finding-all-the-subsets-of-a-set) Also see this [How to generate all permutations of a string in PHP?](http://stackoverflow.com/questions/2617055/how-to-generate-all-permutations-of-a-string-in-php) So, search first! – miqbal Apr 27 '12 at 21:56
  • 2
    This is the kind of thing you should try first on your own, imo - bear in mind some problems do not have "a script" that you can just slot into place. One algorithm is: list all the permutations of length=1 (easy). Then list all the permutations of length=2 - to do so, take each letter in turn and add one of the remaining letters (for all remaining letters). And so on and so forth - this is quite generalisable. If you give it a go, someone will help you. – halfer Apr 27 '12 at 22:04
  • 1
    Are you trying to create a brute script? It certainly looks like it. :P – Starx Apr 27 '12 at 22:08
  • http://stackoverflow.com/questions/10130743/all-possible-combinations-string-speed is a good example of how things like this can easily get out of hand :) – leemeichin Apr 27 '12 at 22:10
  • 1
    @Starx: Lol, either that or homework. – Mike Purcell Apr 27 '12 at 22:12
  • have you considered, even only for a brief moment of weakness, writing one? – ZJR Apr 27 '12 at 22:16
  • 1
    You should take into account that the resultset will be `S(n!)[1->n]` large, meaning, for a 12 letter string, you'll get 522956313 results, which is approximately 6.23GB of memory allocation. Beware! – Madara's Ghost Apr 27 '12 at 22:18
  • http://stackoverflow.com/questions/2617055/how-to-generate-all-permutations-of-a-string-in-php – semsem Apr 27 '12 at 22:21

2 Answers2

2

Here is a modified function from this answer

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 = "12a";
$len = strlen($str);
for($i =0; $i <= $len; $i++) {
   permute($str,0,$i + 1); // call the function.
}
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
1

This is not perfect, because your output set is not well-defined. First, figure out what your output set should look like, then use below as a way to get started.

<?php

$input = "12a";
$input_array = str_split($input, 1);//get an array of each individual character

$max_length = strlen($input);
$length = 01;
$result = array();

foreach($input_array as $character) {
  $result[] = $character;
}

while ($length < $max_length){
  foreach($result as $substring) {
    foreach($input_array as $character) {
      $result[] = $substring.$character;
    }
  }
  $length++;
}

foreach ($result as $result_string) {
  echo $result_string.", ";
}

As a note, in general these sorts of algorithms make use of 'dynamic programming'.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102