Print all the permutations of a string in C

前端 未结 8 1471
自闭症患者
自闭症患者 2020-11-29 04:06

I am learning backtracking and recursion and I am stuck at an algorithm for printing all the permutations of a string. I solved it using the bell algorithm for permutation b

8条回答
  •  佛祖请我去吃肉
    2020-11-29 04:53

    def perms(s):
        if len(s) < 1:
            return [s]
        ps = []
        for i in range(0, len(s)):
            head, tail = s[i], s[0:i] + s[i + 1:]
            ps.extend([head + tailp for tailp in perms(tail)])
        return ps
    

提交回复
热议问题