如何在 PHP 中生成字符串的所有排列?
我需要一种算法来返回一个字符串中所有字符的所有可能组合。
我已经尝试过:
$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
。
I need an algorithm that return all possible combination of all characters in one string.
I've tried:
$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;
}
But that only returns the same amount combination as the length of the string.
Say the $input = "hey"
, the result would be: hey, hye, eyh, ehy, yhe, yeh
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用基于回溯的方法来系统地生成所有排列:
输出:
You can use a back tracking based approach to systematically generate all the permutations:
Output:
我的变体(也适用于数组或字符串输入)
PS:反对者,请解释您的立场。此代码使用了额外的
str_split
和array_diff_key
标准函数,但此代码片段是最小,它实现了纯粹的尾递归 只有一个输入参数,并且它与输入数据类型同构。也许与其他实现相比,它会失去一些基准(但性能实际上与 @codaddict 对几个字符串的答案几乎相同),但为什么我们不能只将其视为具有其自身特点的不同替代方案之一自己的优势?
My variant (works as well with array or string input)
P.S.: Downvoter, please explain your position. This code uses additional
str_split
andarray_diff_key
standard functions, but this code snippet is the smallest, it implements pure tail recursion with just one input parameter and it is isomorphic to the input data type.Maybe it will lose benchmarks a little when comparing with other implementations (but performance is actually almost the same as in @codaddict's answer for several character strings), but why we can't we just consider it as one of the different alternatives which has its own advantages?
我会将所有字符放入一个数组中,并编写一个递归函数来“删除”所有剩余字符。如果数组为空,则将引用传递给数组。
打印出来:
啊是的,
组合=顺序并不重要。
排列=顺序很重要。
所以嘿,hye yeh 都是相同的组合,但是如上所述有 3 种不同的排列。请注意,物品的规模增长得非常快。它叫做阶乘,写成 6! = 6*5*4*3*2*1 = 720 项(对于 6 个字符的字符串)。 10 个字符的字符串将是 10! = 已经有3628800个排列,这是一个非常大的数组。在本例中为 3! = 3*2*1 = 6。
I would put all the characters in an array, and write a recursive function that will 'stripe out' all the remaining characters. If the array is empty, to a reference passed array.
Prints out:
Ah yes,
combinations = order doens't matter.
permutations = order does matter.
So hey, hye yeh are all the same combination, but 3 separate permutations as mentioned. Watch out that the scale of items goes up very fast. It's called factorial, and is written like 6! = 6*5*4*3*2*1 = 720 items (for a 6 character string). A 10 character string will be 10! = 3628800 permutations already, which is a very big array. In this example it's 3! = 3*2*1 = 6.
我的方法使用递归并且没有循环,请检查并提供反馈:
My approach uses recursion and no loops, please check and give feedback:
我创建了一个简单的类,它使用 生成器 来创建排列这样你就可以迭代所有可能的组合而不会耗尽内存。
该类可以采用字符串或数组,
并返回一个 Generator 对象,可以使用
foreach
对其进行迭代。显然,字符串或数组越长,生成所有排列所需的时间就越长。
这是针对 PHP 7.4 构建的
I made a simple class that uses Generators to create the permutations.This way you can just iterate over all possible combinations without exhausting the memory.
The class can take either a string or an array,
and returns a Generator object which can be iterated over with
foreach
.Obviously the longer the string or array, the longer it takes to generate all the permutations.
This has been build against PHP 7.4
这对我有用
This worked for me