-2

How to re-arrange string so that same characters are not next to each other and if there are many alternative sorting options we'll choose the one which is alphabetically sorted?

i.e.

AAABBBB -> BABABAB
AAABBB -> ABABAB
BCDDEEEF -> BCEDEDEF
BACHH -> ABHCH

Pseudo code or something would be useful.

wl1Z
  • 11
  • 3

1 Answers1

1

A naive solution:

Find all permutations of the string
Find all that don't have repeating characters
Find the first alphabetically
clcto
  • 9,530
  • 20
  • 42
  • +1 for great simple solution. Could be a tad slow though. – Floris Sep 05 '14 at 20:20
  • 3
    This solution will be prohibitively expensive on any reasonable-sized string. It's great to have a naive solution available, but I doubt this is as efficient as it can be. – templatetypedef Sep 05 '14 at 21:04