#K75032. Bead Arrangement Challenge
Bead Arrangement Challenge
Bead Arrangement Challenge
You are given N beads, each having a color represented by a lowercase letter. Your task is to determine whether it is possible to arrange these beads in a line such that no two adjacent beads share the same color.
The problem can be tricky because if one color appears too frequently, it might be impossible to separate them sufficiently. Formally, let \( c_i \) be the count of beads of a certain color. A valid arrangement exists if and only if:
[ \max\limits_i { c_i } \leq \left\lfloor \frac{N+1}{2} \right\rfloor ]
If a valid arrangement exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from stdin and consists of two lines. The first line contains a single integer N (the number of beads). The second line contains a string of length N where each character represents the color of a bead.
outputFormat
The output is written to stdout. Print a single line containing either YES
if it is possible to arrange the beads such that no two adjacent beads have the same color, or NO
otherwise.
5
aabbc
YES