Reorganize String - Complete Solution Guide
Reorganize String is LeetCode problem 767, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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.
Detailed Explanation
The problem asks us to rearrange the characters of a given string 's' such that no two adjacent characters are the same. The goal is to return any possible rearrangement that satisfies this condition. If it's impossible to rearrange the string in this way, we should return an empty string.
Solution Approach
The solution uses a greedy approach combined with a priority queue (or sorting in the C implementation). First, it counts the frequency of each character in the input string. If any character appears more than (n + 1) / 2 times, it's impossible to rearrange, so it returns an empty string. Otherwise, it uses a priority queue (or sorts) to store characters and their counts, ordered by frequency. It then iterates, adding the most frequent character at the next available index in the `ans` array. It prioritizes placing characters at even indices first (0, 2, 4, ...). If the end of the array is reached, it starts again at index 1 to continue filling the array. The resulting character array is then converted into a string and returned.
Step-by-Step Algorithm
- Step 1: Count the frequency of each character in the input string 's'.
- Step 2: Check if any character's frequency is greater than (n + 1) / 2. If yes, return an empty string.
- Step 3: Create a priority queue (or sort the character counts). The priority queue should store characters and their counts, ordered by the count in descending order.
- Step 4: Create a character array 'ans' of the same size as the input string 's'.
- Step 5: Iterate while the priority queue is not empty.
- Step 6: For each character in the priority queue, place it into the 'ans' array. Start by placing it at index 'idx', then increment 'idx' by 2. If 'idx' goes beyond the length of the string, reset 'idx' to 1.
- Step 7: Convert the character array 'ans' to a string and return it.
Key Insights
- Insight 1: The most frequent character in the string can appear at most (n + 1) / 2 times, where n is the length of the string. Otherwise, it's impossible to rearrange the string without adjacent characters being the same.
- Insight 2: Using a priority queue (or max-heap) to keep track of the most frequent characters is efficient for placing characters in the rearranged string.
- Insight 3: Distributing the most frequent characters first and placing them at even indices, and then shifting to odd indices if necessary, helps in avoiding adjacent duplicates.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, String, Greedy, Sorting, Heap (Priority Queue), Counting.
Companies
Asked at: Citadel, Coupang, Druva, Intuit, PayPal, Pinterest, Roblox, Tesla, Walmart Labs, eBay.