题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
class Solution:
def __init__(self):
self.res=[]
def PermutionCore(self,ss,begin):
if begin==len(ss):
self.res.append(ss)
return
for i in range(begin,len(ss)):
if i != begin and ss[i]==ss[begin]:
continue
str_list=list(ss)
str_list[i],str_list[begin]=str_list[begin],str_list[i]
ss=''.join(str_list)
self.PermutionCore(ss,begin+1)
def Permutation(self, ss):
# write code here
if len(ss)==0:
return []
self.PermutionCore(ss,0)
self.res.sort()
return self.res

来源:https://www.cnblogs.com/zhaiyansheng/p/10417295.html