0
char ary[10] = "AAABB";

There are 6!/(3!*2!) possible ways to arrange them. How do I find them all?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Byron
  • 9
  • 4
  • You should take a look at this: http://stackoverflow.com/questions/2710713/algorithm-to-generate-all-possible-permutations-of-a-list – Rok Novosel Nov 26 '16 at 15:58

1 Answers1

1

First, there are 5! / (3! * 2!) = 10 possible ways, not 6! / (3! * 2!). But I think this is your typo.

For this specific string "AAABB", you can do something like this:

Take away "BB", and treat "A"s as separators of "B" slots. Let _ (underscore) be the slot we can insert "B" in.

_A_A_A_

First, treat "BB" as two separated char, and insert them in. We can insert them in slot 1 & 2 ("BABAA"), 1 & 3 ("BAABA"), 1 & 4, 2 & 3, 2 & 4, 3 & 4. (6 in total)

Then, treat "BB" as one, and insert it in. We can insert it in slot 1 ("BBAAA"), 2 ("ABBAA"), 3, 4. (4 in total)

All 10 possible ways iterated.

Zhigang An
  • 296
  • 2
  • 13