题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
from enum import unique # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param str string字符串 # @return string字符串一维数组 # class Solution: def Permutation(self , str: str) -> List[str]: # write code here n = len(str) if n == 0: return [""] if n == 1: return [str] if n == 2: if str != str[::-1]: return [str, str[::-1]] else: return [str] res = [] flags = {} for i in range(n): if str[i] not in flags.keys(): flags[str[i]] = 1 temp = [str[j] for j in range(n) if j != i] ss = ''.join(temp) res.extend([str[i]+p for p in self.Permutation(ss)]) return res