#C7177. Gearbot Sequences
Gearbot Sequences
Gearbot Sequences
You are given a series of strings. For each string, determine if it is possible to rearrange the characters to form an alternating sequence using exactly two distinct types of gears. An alternating sequence is defined as one in which no two adjacent characters are the same. In order for such a sequence to be possible, the string must consist of exactly two unique characters and the count difference between them must be at most 1.
For example:
- "abba": Contains exactly two distinct characters and counts are equal, so it can be rearranged as "abab" (YES).
- "abcabc": Contains three distinct characters, so rearrangement is not possible (NO).
- "aaabbb": Exactly two distinct characters with equal count can form an alternating sequence (YES).
- "abccba": Contains three distinct characters (NO).
Your task is to read the input from standard input and for each string, print "YES" if the string can be rearranged to form such an alternating sequence, or "NO" otherwise.
inputFormat
The first line of input contains a single integer T, denoting the number of test cases. Each of the following T lines contains a non-empty string S consisting of lowercase English letters. The string S may be empty as a special case and should be considered invalid for forming an alternating sequence.
outputFormat
For each test case, output a single line with either "YES" if the string can be rearranged into an alternating sequence using exactly two distinct characters (with counts differing by at most 1), or "NO" otherwise.
## sample4
abba
abcabc
aaabbb
abccba
YES
NO
YES
NO
</p>