LeetCode 767: Reorganize String Solution

Master LeetCode problem 767 (Reorganize String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To rearrange the characters of the input string such that no two adjacent characters are the same, we can follow the steps below:

  1. Count the frequency of each character in the string.
  2. Use a priority queue (max heap based on character frequency) to build the rearranged string.
  3. Pop two characters with the highest frequency from the priority queue, append them to the result string, and decrease their frequency.
  4. Continue this process until the priority queue is empty or there is only one character left.
  5. If there is still a character left in the priority queue, check if its frequency is greater than 1 (meaning there are adjacent characters with the same value). If so, return an empty string. Otherwise, append this character to the result string.

Time Complexity: O(nlogn) where n is the length of the input string s. Space Complexity: O(n) where n is the length of the input string s.

:

Solution Code

import java.util.*;

class Solution {
    public String reorganizeString(String s) {
        int n = s.length();
        int[] count = new int[26];
        for (char c : s.toCharArray()) {
            count[c - 'a']++;
        }
        
        PriorityQueue<Character> pq = new PriorityQueue<>((a, b) -> count[b - 'a'] - count[a - 'a']);
        for (int i = 0; i < 26; i++) {
            if (count[i] > 0) {
                pq.offer((char) (i + 'a'));
            }
        }
        
        StringBuilder sb = new StringBuilder();
        
        while (pq.size() >= 2) {
            char first = pq.poll();
            char second = pq.poll();
            sb.append(first);
            sb.append(second);
            count[first - 'a']--;
            count[second - 'a']--;
            if (count[first - 'a'] > 0) {
                pq.offer(first);
            }
            if (count[second - 'a'] > 0) {
                pq.offer(second);
            }
        }
        
        if (!pq.isEmpty()) {
            char last = pq.poll();
            if (count[last - 'a'] > 1) {
                return "";
            }
            sb.append(last);
        }
        
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 767 (Reorganize String)?

This page provides optimized solutions for LeetCode problem 767 (Reorganize String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 767 (Reorganize String)?

The time complexity for LeetCode 767 (Reorganize String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 767 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 767 in Java, C++, or Python.

Back to LeetCode Solutions