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.
题目给定一个字符串,要求重排字符串里的字符使得任意两个相邻的字符都是不同的字符,返回重排后的字符串,如果无法做到就返回一个空字符串。
很明显,如果有某一个字符的出现频率超过总个数的一半(总个数n为偶数时一半是n//2, 为奇数时一半规定为n//2 + 1, 统一起来就是频率大于(n + 1) // 2),那么不管怎么重排都至少有一对相邻的字符是相同的。因此,首先需要保证所有字符的个数都要不超过总个数的一半(<&