Skip to main content

Reorganize String


Reorganize String: Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.

Return any possible rearrangement of s or return "" if not possible.

Example 1:
Input: s = "aab"
Output: "aba"

Example 2:
Input: s = "aaab"
Output: ""

Constraints:
  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Try this Problem on your own or check similar problems:

  1. Longest Happy String
  2. Task Scheduler
Solution:
public String reorganizeString(String s) {
Map<Character, Integer> map = new HashMap<>();
IntStream.range(0, s.length()).forEach(i -> map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1));

PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
pq.addAll(map.entrySet());
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();

StringBuilder sb = new StringBuilder();

while(!pq.isEmpty()){
Map.Entry<Character, Integer> entry = pq.poll();

if(!sb.isEmpty() && sb.charAt(sb.length() - 1) == entry.getKey()) return "";

sb.append(entry.getKey());

if(waitQueue.size() > 0){
pq.add(waitQueue.poll());
}

if(entry.getValue() - 1 == 0){
map.remove(entry.getKey());
}
else {
map.replace(entry.getKey(), entry.getValue() - 1);
waitQueue.add(entry);
}

}

return waitQueue.isEmpty() ? sb.toString() : "";
}

Time/Space Complexity:
  • Time Complexity: O(nlogn)
  • Space Complexity: O(n)

Explanation:

This is a generalized solution to reorganizing string so that we can have gap x between same characters (for our case x = 1). We build a frequency map to track the occurrence of each character in string and we build a maxheap so that we have most frequent characters at top (note this a greedy approach just like we had in the solution for Task Scheduler). We also create a waitQueue after we used a character, we place it in the waiting queue so we can reuse it again after we filled the gap x with different characters (note that you could make the waiting queue wait for x > 1, in that scenario we would have a larger gap between same characters). If we have the same character at top of heap and last placed character in the new reorganized string, we know we can't make the arrangement of having a gap between same characters, so we return empty string. After we have placed the character in new string we check if we have placed all occurrences of that character and if we did, we remove it from the frequency map, otherwise we just decrement it's frequency and place it in the waiting queue again. We return the reorganized string only if we have placed all characters back and the waiting queue is empty (valid arrangement).