繁体   English   中英

给定字符串的所有可能组合

[英]All possible combinations of a given string

我需要找到给定字符串的所有可能组合,从最小长度到最大长度。

interface allCombos(string: String, min: Number, max:Number): Array {}

因此,如果我的输入字符串为'abcde' ,并且我的最小长度为3,我希望结果为:

对于长度3:

[‘abc’, ‘abd’, ‘abe’, ‘acd’, ..., ‘bcd’, ‘bce’, ..., ‘eda’, ...]

与长度4串联:

[‘abcd’, ‘abdc’, ‘acdb’, ‘acbd’, …etc]

长度不超过max参数的所有可能组合。 不应超过输入字长。

我开始认为所有可能的组合都是∑(3! + 4! + … + n!) 但是后来我发现我错了,因为对于每个长度的子集,整个世界都有许多组合(例如6字母字符串的3长度组合)。

社区可以帮助我解决这个问题吗?

解决方案可以是JavaScriptPython甚至是伪代码。

编辑

为了知识。 有人能回答我吗,这种情况下描述结果大小的公式? 我知道它不是∑(3! + 4! + … + n!)

您可以为此使用itertools.combinations

from itertools import combinations

["".join(li) for li in combinations('abcde', 3)]

这将给

['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde', 'cde']

简要说明:

list(combinations('abcde', 3))

会给

[('a', 'b', 'c'),
 ('a', 'b', 'd'),
 ('a', 'b', 'e'),
 ('a', 'c', 'd'),
 ('a', 'c', 'e'),
 ('a', 'd', 'e'),
 ('b', 'c', 'd'),
 ('b', 'c', 'e'),
 ('b', 'd', 'e'),
 ('c', 'd', 'e')]

因此,您的三个字母的所有组合。 您可以将单个元组加入列表理解中。

如果愿意,您当然可以轻松地将其循环:

min_length = 3
max_length = 5

res = {str(i): ["".join(li) for li in combinations('abcde', i)] for i in range(min_length, max_length + 1)}

这给

{'3': ['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde', 'cde'],
 '4': ['abcd', 'abce', 'abde', 'acde', 'bcde'],
 '5': ['abcde']}

如果您希望将其包含在一个列表中:

import numpy as np
final_list = np.concatenate(res.values())

产生

array(['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde',
       'cde', 'abcde', 'abcd', 'abce', 'abde', 'acde', 'bcde'],
      dtype='|S5')

我很高兴向您介绍精彩的python标准库itertools 您将要使用组合功能。 这个库的很棒之处在于它解决了几乎所有组合循环问题。

 import itertools

 min_length = 2
 max_length = 5

 s = 'ABCDEF'
 for i in range(min_length, max_length):
     print('length', i, 'cominations')
     print([_ for _ in itertools.combinations(s, i)])

其他人向您展示了一些不错的组合/排列选项,但我认为您的完全预期输出是这样的:

from itertools import combinations

def allCombos(str, min_length, max_length):
    str_combinations = []
    for length in range(min_length, max_length):
        combinations = [''.join(c) for c in combinations(str, length)]
        str_combinations.append(combinations)
    return str_combinations

[编辑]:为了知识。 有人能回答我吗,这种情况下描述结果大小的公式? 我知道它不是∑(3!+ 4!+…+ n!)。

下面找到三种数学方法,它们可以使用JavaScript提供相同的最终结果。 有关该过程的更多说明,请参见

本主题内的其他感兴趣的项目

 const n = 4; { console.log("multiplication in loop:\\n"); let l = 1, k; for (let i = k = n; l < i; k *= l++); console.log({ [`${n}!`]: k }); } { console.log("single multiplication 4 * 3 * 2 * 1:\\n"); console.log({ [`${n}!`]: 4 * 3 * 2 * 1 }); } { console.log("multiplication in steps:\\n"); let curr = n; let acc = {}; acc[`${curr} * ${curr - 1}`] = curr * --curr; console.log(acc); acc[`${Object.keys(acc).pop()} * ${--curr}`] = curr * +acc[Object.keys(acc).pop()]; console.log(acc); acc[`${Object.keys(acc).pop()} * ${--curr}`] = curr * +acc[Object.keys(acc).pop()]; console.log(acc); } 

暂无
暂无

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

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