字母之间点的所有可能位置的算法

Algorithm for all the possible positions of dots between letters

假设我有一个像 "welcome" 这样的词,我想在该词的字母之间插入一个句号。 所以它会是 "welcome", "w.elcome", we.lcome",......直到达到 "w.e.l.c.o.m.e".

我需要一个算法来为我提供任何给定单词的所有可能的字母和句号组合。

考虑包含 . 所有可能位置的集合 S。现在,集合 S 的长度将是 lengthOfTheString - 1.

现在您只需要找到集合 S 的所有可能子集,并在处理每个子集时,您可以将子集中存在的位置标记为 .

How to generate a powerset.

这样你就可以生成所有可能的组合。

Ex. String : "abc"

a b c
 ^ ^
 1 2

Subsets :
{}       abc
{1}      a.bc
{2}      ab.c
{1,2}    a.b.c