How to print all permutations of String both iterative and Recursive way? VB.NET

后端 未结 1 1083
暗喜
暗喜 2021-01-27 13:42

Can any one help me how to print all permutations of String both iterative and Recursive way? I prefer VB.NET or just pseudo code is fine.

Thanks in advance.

1条回答
  •  无人及你
    2021-01-27 14:10

    Recursive approach is as follows:

    Base case: The permutation of 1 letter is one element.

    General case: The permutation of a set of letters is a list each of the letters, concatenated with every permutation of the other letters.

    Explanation:

    If the set just have one letter then return that letter. permutation(a) -> a

    If the set has two letters, for each letter return it and the permutation of the rest of the letters.

    permutation(ab) ->

    a + permutation(b) -> ab

    b + permutation(a) -> ba

    For each letter in set return the letter concatenated with perumation of the rest of the set of letters.

    permutation(abc) ->

    a + permutation(bc) --> abc, acb

    b + permutation(ac) --> bac, bca

    c + permutation(ab) --> cab, cba

    0 讨论(0)
提交回复
热议问题