簡體   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