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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for reorganizing a string involves trying every single possible arrangement of the letters. We check each arrangement to see if it meets the requirement that no two adjacent letters are the same. If an arrangement works, we return it; otherwise, we keep trying different arrangements.
Here's how the algorithm would work step-by-step:
def reorganize_string_brute_force(input_string):
import itertools
all_permutations = itertools.permutations(input_string)
for permutation in all_permutations:
string_permutation = ''.join(permutation)
# Check for adjacent identical characters
is_valid = True
for index in range(len(string_permutation) - 1):
if string_permutation[index] == string_permutation[index + 1]:
is_valid = False
break
# Return current string if valid
if is_valid:
return string_permutation
# If no valid string found return empty
return ""
To rearrange a string so that no two identical characters are next to each other, we focus on distributing the most frequent characters first. This prevents having a large cluster of identical characters that are impossible to separate. The main idea is to prioritize characters based on their frequency to create a valid arrangement.
Here's how the algorithm would work step-by-step:
def reorganize_string(input_string):
character_counts = {}
for char in input_string:
character_counts[char] = character_counts.get(char, 0) + 1
sorted_characters = sorted(character_counts.items(), key=lambda item: item[1], reverse=True)
reorganized_string = [''] * len(input_string)
index = 0
# Distribute most frequent characters first
for char, count in sorted_characters:
for _ in range(count):
if index >= len(input_string):
index = 1
# If no valid arrangement is possible
if index >= len(input_string):
return ""
reorganized_string[index] = char
index += 2
return ''.join(reorganized_string)
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or handle as an invalid input by throwing an exception. |
String with only one character | Return the original single-character string as it's already reorganized. |
String where one character's count is more than (n+1)/2 | Return an empty string since a valid reorganization is impossible in this case. |
String with all identical characters | Return an empty string as it's impossible to reorganize such a string. |
Very long input string (performance) | Ensure the solution uses an efficient data structure (e.g., priority queue) to avoid quadratic time complexity. |
String with only two distinct characters | If their counts differ by more than one, return an empty string, otherwise reorganize them alternatingly. |
String with a balanced character distribution (close to equal counts) | Ensure the algorithm correctly interleaves the characters even when the counts are very similar. |
String with Unicode characters | The character counting mechanism must correctly handle extended character sets beyond basic ASCII. |