从给定字符串中删除 k 个字符后,如何找到所有可能的字符串?
How to find all possible strings after deleting k number of characters from the given string?
例如我们有一个字符串 s = "Whosebug" 。如果我们删除 一个 字符那么我们得到 "tackoverflow", "sackoverflow", "stckoverflow" ...等等。
当我们删除 k < len(s) 的 k 个字符时,如何获得所有可能的字符串。
我们可以通过 for 循环来确定要删除的确切字符数,string.But当要删除的字符数不固定时如何做。
itertools
是你的朋友:
from itertools import combinations
s = "stack overflow"
n_delete = 1
print([''.join(i) for i in combinations(s, len(s) - n_delete)])
例如我们有一个字符串 s = "Whosebug" 。如果我们删除 一个 字符那么我们得到 "tackoverflow", "sackoverflow", "stckoverflow" ...等等。 当我们删除 k < len(s) 的 k 个字符时,如何获得所有可能的字符串。
我们可以通过 for 循环来确定要删除的确切字符数,string.But当要删除的字符数不固定时如何做。
itertools
是你的朋友:
from itertools import combinations
s = "stack overflow"
n_delete = 1
print([''.join(i) for i in combinations(s, len(s) - n_delete)])