编写一个方法,确定某字符串的所有排列组合。
给定一个string A和一个int n,代表字符串和其长度,请返回所有该字符串字符的排列,保证字符串长度小于等于11且字符串中字符均为大写英文字符,排列中的字符串按字典序从大到小排序。(不合并重复字符串)
测试样例:
"ABC"
返回:["CBA","CAB","BCA","BAC","ACB","ABC"]
- 递归方法 求出全排列 最后再排序
from copy import copy
class Permutation:
result = []
def getPermutation(self, A):
self.result = []
self.get(list(A))
self.result.sort(reverse=True)
return self.result
def swap(self, A, i , j):
t = A[i]
A[i] = A[j]
A[j] = t
def get(self, A, start=0):
if start == len(A)-1:
self.result.append(''.join(A))
for i in range(start, len(A)):
self.swap(A, start, i)
self.get(A, start+1)
self.swap(A, start, i)