字符串的排列(important)

题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:

输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

python solution:

# -*- coding:utf-8 -*-
class Solution:
    def Permutation(self, ss):
        if not ss:
            return []
        def Permutation(startIdx):
            if startIdx >= len(arrSs):
                clone = ''.join(arrSs)
                res.append(clone)
            else:
                changeIdx = startIdx
                while changeIdx < len(arrSs):
                    arrSs[changeIdx], arrSs[startIdx] = arrSs[startIdx], arrSs[changeIdx]
                    Permutation(startIdx + 1)
                    arrSs[changeIdx], arrSs[startIdx] = arrSs[startIdx], arrSs[changeIdx]
                    changeIdx += 1
        res = []
        arrSs = list(ss)
        Permutation(0)
        res = list(set(res))
        return sorted(res)

猜你喜欢

转载自www.cnblogs.com/bernieloveslife/p/10426892.html
今日推荐