Practice/Amazon/Leetcode 767. Reorganize String
CodingMust
Given a string containing lowercase letters, rearrange its characters so that no two adjacent characters in the result are identical. If multiple valid arrangements exist, return any one of them. If no valid arrangement is possible, return an empty string.
Your task is to determine whether such a rearrangement exists and construct one if possible.
""Example 1:
Input: s = "aabbcc" Output: "abcabc" Explanation: We can alternate between the three different characters. Other valid outputs include "ababcc", "bacbac", etc.
Example 2:
Input: s = "aaab" Output: "" Explanation: The character 'a' appears 3 times in a string of length 4. Since (4+1)/2 = 2.5, no character can appear more than 2 times, making a valid arrangement impossible.
Example 3:
Input: s = "aab" Output: "aba" Explanation: We can place 'a' characters at positions 0 and 2, with 'b' at position 1.