题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
动态规划,17行代码,python
这个题很坑啊,题目要求说不需要顺序,测试用例要顺序对才通过。。。
import copy
class Solution:
    def Permutation(self, ss):
        # write code here
        alist=[list(ss[0])]
        for s in ss[1:]:
            aa=[]
            for l in alist:
                for j in range(len(l)+1):
                    b=copy.copy(l)
                    b.insert(j,s)
                    if b not in aa:
                        aa.append(b)
            alist=aa
        for i in range(len(alist)):
            j=''.join(alist.pop(0))
            alist.append(j)
        return sorted(alist)
