#K43587. Can Sort Blocks
Can Sort Blocks
Can Sort Blocks
You are given a string S
consisting of lowercase letters, where each letter represents a colored block. Your task is to determine if it is possible to rearrange the blocks so that no two adjacent blocks have the same color.
The necessary and sufficient condition is that the maximum frequency of any block does not exceed \(\lceil \frac{|S|}{2} \rceil\). If this condition holds, then a valid rearrangement exists and you should output YES
. Otherwise, output NO
.
Example:
- For
S = "abac"
, one valid arrangement is "aabc" (or any other valid rearrangement), so the answer isYES
. - For
S = "aaaa"
, it is impossible to rearrange the blocks to avoid identical adjacent characters, so the answer isNO
.
inputFormat
A single line containing a non-empty string S
composed of lowercase English letters.
outputFormat
Output a single line containing YES
if it is possible to rearrange the blocks so that no two adjacent blocks are the same, otherwise output NO
.## sample
abac
YES