面试题 08.08. 有重复字符串的排列组合
题目描述
有重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合。
示例1:
输入:S = "qqe"
输出:["eqq","qeq","qqe"]
示例2:
输入:S = "ab"
输出:["ab", "ba"]
代码和思路
class Solution(object):
def permutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
return ["".join(e) for e in set(itertools.permutations(S))]
"""
itertools.permutations(S) 就是返回可迭代对象的所有数学全排列方式。
for item in permutations(['a', 'b', 'c']):
... print item
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
"""

浙公网安备 33010602011771号