Swap through an array of characters

Suppose you need to detect all possible permutations of 'n' different characters, for example 'a', 'b', 'c'. Can you suggest an algorithm that I can use to do this? Generally speaking, how would you do this?

+3
source share
2 answers

Let 'Perms' be the set of permutations found, and 'Used' the list of selected characters.

Find permutations of n characters from the set S:

  • If n = 0, then Used is a permutation. Add it to Perm and return.
  • For every char C in S:
    • Remove C from S and add (or press) it to U.
    • Find permutations of n-1 characters from S.
    • ( ) U C S.

n , Perms .

, . , , .

+1

Java .

+1

Source: https://habr.com/ru/post/1755428/


All Articles