#K88477. Alternating Pattern Batch Checker

    ID: 37317 Type: Default 1000ms 256MiB

Alternating Pattern Batch Checker

Alternating Pattern Batch Checker

You are given a batch of product codes, each consisting only of the characters 'A' and 'B'. Your task is to determine if it is possible to rearrange the characters of the batch into an alternating pattern (for example, "ABAB..." or "BABA...").

A batch can be rearranged into an alternating pattern if and only if the absolute difference between the number of 'A's and 'B's is at most 1, i.e. countAcountB1|count_A - count_B| \le 1.

For multiple batches provided as input, you must check each one and output "YES" if an alternating pattern is possible, otherwise output "NO".

inputFormat

The first line of the input contains an integer T, the number of batches. Each of the following T lines contains a string consisting solely of 'A' and 'B', representing a batch of product codes.

outputFormat

For each test case, output a single line containing either "YES" if the batch can be rearranged into an alternating pattern, or "NO" if it cannot.## sample

5
AABB
AB
AAAB
BBBB
AAAAAABBBBB
YES

YES NO NO YES

</p>