Print all the permutations of a string in C

前端 未结 8 1474
自闭症患者
自闭症患者 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:51

    The algorithm basically works on this logic:

    All permutations of a string X is the same thing as all permutations of each possible character in X, combined with all permutations of the string X without that letter in it.

    That is to say, all permutations of "abcd" are

    • "a" concatenated with all permutations of "bcd"
    • "b" concatenated with all permutations of "acd"
    • "c" concatenated with all permutations of "bad"
    • "d" concatenated with all permutations of "bca"

    This algorithm in particular instead of performing recursion on substrings, performs the recursion in place on the input string, using up no additional memory for allocating substrings. The "backtracking" undoes the changes to the string, leaving it in its original state.

提交回复
热议问题