Generating all permutations of a given string

前端 未结 30 1682
我寻月下人不归
我寻月下人不归 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:40

    Use recursion.

    • Try each of the letters in turn as the first letter and then find all the permutations of the remaining letters using a recursive call.
    • The base case is when the input is an empty string the only permutation is the empty string.

提交回复
热议问题