27.字符串的排列
问题:
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
解决:
思想:
这道题我们直接使用python中的迭代器,里面的permutations函数可以根据输入的字符,进行排列。
python代码:
# -*- coding:utf-8 -*-
from itertools import permutations
class Solution:
def Permutation(self, ss):
# write code here
if not ss:
return []
return sorted(list(set(map(''.join,permutations(ss)))))