#K12136. Rearranging Characters to Avoid Adjacent Duplicates

    ID: 23624 Type: Default 1000ms 256MiB

Rearranging Characters to Avoid Adjacent Duplicates

Rearranging Characters to Avoid Adjacent Duplicates

Given a string S, your task is to determine if it is possible to rearrange its characters such that no two adjacent characters are the same.

For a string of length ( n ), let ( f_{max} ) be the maximum frequency of any character in S. A necessary and sufficient condition for such a rearrangement to exist is:
( f_{max} \leq \left\lceil \frac{n}{2} \right\rceil ).

If the condition is met, output YES; otherwise, output NO.

inputFormat

The input consists of a single line containing the string S. The string S is composed only of lowercase English letters.

outputFormat

Output a single line: YES if it is possible to rearrange S so that no two adjacent characters are the same; otherwise, output NO.## sample

aaabb
YES

</p>