42

我需要一种算法来返回一个字符串中所有字符的所有可能组合。

我试过了:

$langd = strlen($input);
 for($i = 0;$i < $langd; $i++){
     $tempStrang = NULL;
     $tempStrang .= substr($input, $i, 1);
  for($j = $i+1, $k=0; $k < $langd; $k++, $j++){
   if($j > $langd) $j = 0;
   $tempStrang .= substr($input, $j, 1);
 }
 $myarray[] = $tempStrang;
}

但这只会返回与字符串长度相同的数量组合。

$input = "hey",结果将是:hey, hye, eyh, ehy, yhe, yeh

4

4 回答 4

53

您可以使用基于回溯的方法系统地生成所有排列:

// 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.

输出:

#php a.php
hey
hye
ehy
eyh
yeh
yhe
于 2010-04-11T13:06:32.953 回答
28

我的变体(也适用于数组或字符串输入)

function permute($arg) {
    $array = is_string($arg) ? str_split($arg) : $arg;
    if(1 === count($array))
        return $array;
    $result = array();
    foreach($array as $key => $item)
        foreach(permute(array_diff_key($array, array($key => $item))) as $p)
            $result[] = $item . $p;
    return $result;
}

PS:投反对票,请说明你的立场。此代码使用附加str_splitarray_diff_key标准函数,但此代码片段是最小的,它仅使用一个输入参数实现纯尾递归,并且与输入数据类型同构。

与其他实现相比,它可能会失去一些基准(但性能实际上与@codaddict 对几个字符串的回答几乎相同),但为什么我们不能将其视为具有其的不同替代方案之一自己的优势?

于 2013-09-17T15:30:44.177 回答
8

我会将所有字符放入一个数组中,并编写一个递归函数来“删除”所有剩余的字符。如果数组为空,则给一个引用传递的数组。

<?php

$input = "hey";

function string_getpermutations($prefix, $characters, &$permutations)
{
    if (count($characters) == 1)
        $permutations[] = $prefix . array_pop($characters);
    else
    {
        for ($i = 0; $i < count($characters); $i++)
        {
            $tmp = $characters;
            unset($tmp[$i]);

            string_getpermutations($prefix . $characters[$i], array_values($tmp), $permutations);
        }
    }
}
$characters = array();
for ($i = 0; $i < strlen($input); $i++)
    $characters[] = $input[$i];
$permutations = array();

print_r($characters);
string_getpermutations("", $characters, $permutations);

print_r($permutations);

打印出来:

Array
(
    [0] => h
    [1] => e
    [2] => y
)
Array
(
    [0] => hey
    [1] => hye
    [2] => ehy
    [3] => eyh
    [4] => yhe
    [5] => yeh
)

啊,是的,组合=顺序无关紧要。排列 = 顺序确实很重要。

所以嘿,嘿嘿都是相同的组合,但是如上所述的 3 个单独的排列。注意项目的规模增长非常快。它叫做阶乘,写成 6!= 6*5*4*3*2*1 = 720 项(对于 6 个字符的字符串)。10 个字符的字符串将是 10!= 已经有 3628800 个排列,这是一个非常大的数组。在本例中为 3!= 3 * 2 * 1 = 6。

于 2010-04-11T13:13:09.573 回答
2

我的方法使用递归,没有循环,请检查并提供反馈:

function permute($str,$index=0,$count=0)
{
    if($count == strlen($str)-$index)
        return;

    $str = rotate($str,$index);

    if($index==strlen($str)-2)//reached to the end, print it
    {
        echo $str."<br> ";//or keep it in an array
    }

    permute($str,$index+1);//rotate its children

    permute($str,$index,$count+1);//rotate itself
}

function rotate($str,$index)
{
    $tmp = $str[$index];
    $i=$index;
    for($i=$index+1;$i<strlen($str);$i++)
    {
        $str[$i-1] = $str[$i];
    }
    $str[$i-1] = $tmp;
    return $str;
}
permute("hey");
于 2017-01-02T11:06:35.200 回答