#C555. Rearrange String to Avoid Adjacent Repeated Characters
Rearrange String to Avoid Adjacent Repeated Characters
Rearrange String to Avoid Adjacent Repeated Characters
Given a string s, determine if it is possible to rearrange its characters such that no two adjacent characters are the same. The key observation is that the string can be rearranged if and only if the maximum frequency of any character does not exceed \(\lceil \frac{n}{2} \rceil\), where \(n\) is the length of the string and \(\max(c)\) is the frequency of the most common character.
For example, for the string "aabb", the maximum frequency is 2 and \(\lceil\frac{4}{2}\rceil = 2\), so a rearrangement is possible, and the answer is YES. On the other hand, for "aaab", the maximum frequency is 3 while \(\lceil\frac{4}{2}\rceil = 2\), so a valid rearrangement is not possible, and the answer is NO.
inputFormat
The input consists of a single line that contains the string s. The string will consist of lowercase alphabet letters only.
outputFormat
Output a single line containing 'YES' if it is possible to rearrange the string so that no two adjacent characters are the same, otherwise output 'NO'.## sample
aabb
YES