《剑指Offer(第二版)》面试题38. 字符串的排列

输入一个字符串,打印出该字符串中字符的所有排列。
你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。

示例:
输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]
 
限制:
1 <= s 的长度 <= 8
class Solution:
    def permutation(self, s: str) -> List[str]:
        pre = [s[0]]

        for i in s[1:]:
            new = []
            for j in pre:
                for k in range(len(j)+1):
                    new.append(j[:k]+i+j[k:])
            pre = list(set(new))
        
        return pre

 

你可能感兴趣的:(LeetCode点滴)