#K62812. Reorder Words: Avoid Adjacent Similar Starting Letters
Reorder Words: Avoid Adjacent Similar Starting Letters
Reorder Words: Avoid Adjacent Similar Starting Letters
You are given a list of words for each test case. Your task is to determine whether it is possible to reorder the words such that no two adjacent words start with the same letter.
For each test case, you are given an integer \( n \) which represents the number of words, followed by a line containing the \( n \) words separated by spaces. Let \( f \) be the maximum frequency of any starting letter among these words. It can be proven that a valid reordering exists if and only if \[ f \leq \lceil \frac{n}{2} \rceil \] Otherwise, output NO. If a valid reordering exists, output YES.
Note: You do not actually need to find the ordering; you only need to check whether it is possible.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \( T \) representing the number of test cases.
- For each test case:
- The first line contains an integer \( n \), the number of words.
- The second line contains \( n \) words separated by spaces.
outputFormat
For each test case, output a single line containing either YES
or NO
(without quotes) indicating whether it is possible to reorder the words such that no two adjacent words start with the same letter.
1
3
apple banana cherry
YES
</p>