簡體   English   中英

如何獲得字符保持相同順序的部分編碼字符串的所有可能組合?

[英]How do I get all possible combinations of a partially encoded string where characters remain in same order?

抱歉,標題令人困惑-我不確定如何表達。

我想要的是字符串“hello”的所有組合,其中字符的順序相同,但有些是編碼的。 例如,結果應包括“%68ello”、“h%65llo”、“%68el%6co”、“h%65%6c%6c%6f”等。

如果我沒記錯的話,結果應該是 2⁵ 不同的組合。

如果可能的話,我更喜歡 Python 中的解決方案。

使用遞歸生成器可能會得到很好的結果:

def encodedCombo(S):
    if not S : yield ""; return
    for ss in encodedCombo(S[1:]):
        yield f"%{ord(S[0]):x}"+ss
        yield S[0]+ss

output:

for ec in encodedCombo("hello"): print(ec)

%68%65%6c%6c%6f
h%65%6c%6c%6f
%68e%6c%6c%6f
he%6c%6c%6f
%68%65l%6c%6f
h%65l%6c%6f
%68el%6c%6f
hel%6c%6f
%68%65%6cl%6f
h%65%6cl%6f
%68e%6cl%6f
he%6cl%6f
%68%65ll%6f
h%65ll%6f
%68ell%6f
hell%6f
%68%65%6c%6co
h%65%6c%6co
%68e%6c%6co
he%6c%6co
%68%65l%6co
h%65l%6co
%68el%6co
hel%6co
%68%65%6clo
h%65%6clo
%68e%6clo
he%6clo
%68%65llo
h%65llo
%68ello
hello

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM