[Solved] Algorithm (especially for c++) to show Every Permutation


You want all permutations of each member of the powerset of the input.

permSub("abc", "")

func permSub(input, perm)
  print perm
  if input = "" return

  for i = 0 to input.length-1
    permSub(input[0..i]+input[i+1..input.length), perm+input[i]
  end
end

Where input[i..j] represents the sub string of input from i(inclusive) to j(exclusive), and + is string concatenation.

Note that this will include the empty set, which strictly speaking is correct, but you didn’t include it.

Here’s the original Java implementation and my conversion to C++, which you should not trust 🙂

1

solved Algorithm (especially for c++) to show Every Permutation