#K33567. Rearranging Digits to Avoid Adjacent Duplicates
Rearranging Digits to Avoid Adjacent Duplicates
Rearranging Digits to Avoid Adjacent Duplicates
You are given a string s consisting only of digits. Your task is to determine if you can rearrange the digits of s such that no two adjacent digits are identical.
A necessary and sufficient condition for such a rearrangement to exist is that the maximum frequency of any digit does not exceed \(\lceil \frac{n}{2} \rceil\), where \(n\) is the total number of digits in the string.
If the condition is satisfied, output YES
; otherwise, output NO
.
inputFormat
The input consists of a single line containing a non-empty string of digits. The string may be very long and contains only the digits from 0 to 9.
outputFormat
Output a single line containing either YES
if it is possible to rearrange the digits so that no two adjacent digits are the same, or NO
otherwise.
122221
NO
</p>