代码之家  ›  专栏  ›  技术社区  ›  Johan

如何在PHP中生成字符串的所有排列?

  •  36
  • Johan  · 技术社区  · 16 年前

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

    $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 回复  |  直到 11 年前
        1
  •  51
  •   codaddict    16 年前

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

    // 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
    
        2
  •  27
  •   Alana Storm    10 年前

    我的变量(与数组或字符串输入一起使用)

    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;
    }
    

    下投票者,请解释你的立场。此代码使用附加 str_split array_diff_key 标准函数,但此代码段是 ,它实现了纯 同构的 输入数据类型。

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

        3
  •  7
  •   Hans    16 年前

    <?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
    )
    

    啊是的,

        4
  •  1
  •   Gaurav Pandey    9 年前

    我的方法使用递归和无循环,请检查并给出反馈:

    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");