class Solution:
# 回溯法
def Permutation(self , str: str) -> List[str]:
def backtrack(position):
# 到最后,添加
if position == len(s) - 1:
res.append(''.join(s))
return
# 设置一个dic降重
dic = set()
for index in range(position, len(s)):
if s[index] in dic:
continue
dic.add(s[index])
s[index], s[position] = s[position], s[index]
backtrack(position + 1)
# 交换完要还原,以进行其他可能的交换
s[index], s[position] = s[position], s[index]
res = []
s = list(str)
backtrack(0)
return res

京公网安备 11010502036488号