#K33637. Taco Deletion Problem

    ID: 25132 Type: Default 1000ms 256MiB

Taco Deletion Problem

Taco Deletion Problem

You are given a string S consisting only of characters A and B. You can repeatedly perform the following operation: remove an occurrence of the substring AB from S. Your task is to determine whether it is possible to completely delete all characters from the string by applying this operation any number of times (possibly zero).

In other words, is it possible to obtain an empty string after a sequence of valid deletions?

Hint: The operation removes one A and one B each time. It turns out that the entire string can be deleted if and only if the number of B's is at least the number of A's. Formally, if we denote by $n_A$ the number of A's and by $n_B$ the number of B's in S, then the string can be fully deleted if and only if $n_B \ge n_A$.

inputFormat

The first line contains an integer TT (1T1051 \le T \le 10^5) denoting the number of test cases. Each of the following TT lines contains a non-empty string S consisting only of the characters A and B.

outputFormat

For each test case, output a single line containing either YES if it is possible to delete all characters to obtain an empty string, or NO otherwise.## sample

4
ABB
AAB
ABAB
BABA
YES

NO YES YES

</p>