简体   繁体   English

如何生成字符串的排列,但没有重复和不同的长度?

[英]How to generate permutation of string, but without repeats and different length?

I am looking for a php algorithm or just pseudo code to get all permutations of a string, but without repeat of the characters and the length of generated string can variate from 1 to the maximum length of the input.我正在寻找一种 php 算法或只是伪代码来获取字符串的所有排列,但没有重复的字符,并且生成的字符串的长度可以从 1 到输入的最大长度变化。 Example:例子:

input: 1 2 3输入:1 2 3

possible combinations:可能的组合:

1 1

1 2 / ( it is the same as 2 1, so that must not be calculated) 1 2 /(与2 1相同,所以不能计算)

1 3 1 3

1 2 3 1 2 3

2 2

2 3 2 3

3 3

Here you go :干得好 :

function pc_array_power_set($array) {
    // initialize by adding the empty set
    $results = array(array());

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));

   return $results;

} }

From PHP Cookbook来自 PHP 食谱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM