1

So I need to make a java program that reorganizes string so that there are no two same letters on adjacent places. If possible it would also put the string into alphabetical order. For example

System.out.println(reorganizeString("AAABBB")); would print ABABAB
System.out.println(reorganizeString("CBAED")); would print ABCDE
System.out.println(reorganizeString("AXAXA")); would print AXAXA

I've been trying this or like 4 hours or some, but I just can't seem to be able to craft an algorithm that works with every string. Now I've toyed with the idea of looping the string and removing other character or the adjacent strings and adding it to array and after that is done then I would insert characters from that array to correct places. Like If I have AA then I would remove the other A and add it to the array and keep doing that until the first thing doesn't have any same adjacent characters, then start adding them in to new places from the array. We are allowed to assume that there is solution to all string we use.

Could someone give any tips on how to better do this? Are there some good methods in java that I am not aware of?

Samuli Lehtonen
  • 3,840
  • 5
  • 39
  • 49
  • 4
    [This was asked few minutes ago already](http://stackoverflow.com/q/25660816/572670), be patient and wait for answer instead of multi-posting – amit Sep 04 '14 at 08:48
  • 2
    @amit Well the other person must have been writing it at the same time I did so I didn't really see this was already asked. – Samuli Lehtonen Sep 04 '14 at 09:49
  • 1
    @amit, agreed, it was a few minutes and the content differs a lot, no need to grill the guy – epoch Sep 04 '14 at 10:31

0 Answers0