Generating all permutations of a given string

前端 未结 30 1684
我寻月下人不归
我寻月下人不归 2020-11-21 06:35

What is an elegant way to find all the permutations of a string. E.g. permutation for ba, would be ba and ab, but what about longer st

30条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 06:54

    python implementation

    def getPermutation(s, prefix=''):
            if len(s) == 0:
                    print prefix
            for i in range(len(s)):
                    getPermutation(s[0:i]+s[i+1:len(s)],prefix+s[i] )
    
    
    
    getPermutation('abcd','')
    

提交回复
热议问题