#C7219. Reordering String: Avoid Adjacent Duplicates
Reordering String: Avoid Adjacent Duplicates
Reordering String: Avoid Adjacent Duplicates
You are given a string s consisting of lowercase English letters. Your task is to determine whether it is possible to rearrange the characters of s so that no two adjacent characters are the same.
If such a rearrangement is possible, you should output Yes on the first line and one valid rearranged string on the second line. Otherwise, simply output No.
Consider the following mathematical condition for the existence of a valid rearrangement: \[ \max_{c}\{f(c)\} \leq \left\lceil\frac{|s|}{2}\right\rceil \] where \(f(c)\) represents the frequency of character \(c\) in the string \(s\).
inputFormat
The input consists of a single line containing the string s (only lowercase letters, without spaces).
outputFormat
If a valid rearrangement exists, print Yes
on the first line and the rearranged string on the second line. If not, print No
(without quotes).
aaabb
Yes
ababa
</p>