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?