开发者

Best algorithm for finding the combination of a word? [duplicate]

开发者 https://www.devze.com 2023-01-01 18:20 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: 开发者_JAVA技巧How to find all possible subsets of a given array?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

开发者_JAVA技巧How to find all possible subsets of a given array?

so say you have

ab

you can have a,b,ab

whats the best way of doing this?


From the itertools recipes:

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

This is a standard python module, so reading that should give you insights on how its implemented and what algorithm is used. I don't know if it is the best, but it's an algorithm from the real world.


A power set is one solution

Power set of a set A is the set whose members are all possible subsets of A. For example, the powerset of {1, 2} is { {}, {1}, {2}, {1,2} } .

There's a Python solution:

What algorithm can calculate the power set of a given set?

0

精彩评论

暂无评论...
验证码 换一张
取 消