#K43837. Single Character Transformation

    ID: 27398 Type: Default 1000ms 256MiB

Single Character Transformation

Single Character Transformation

Alex is given a binary string composed of characters '0' and '1'. In one transformation, for each character in the string, if the character is '1', the current balance is increased by 1, and if it is '0', the balance is decreased by 1. The transformation is considered successful if and only if the running balance (i.e. the cumulative sum) always remains in the range \( [-1, 1] \) for every prefix of the string. Your task is to determine for each test case whether the string can be transformed to a single character according to the rule.

For example, consider the string "1100":

  • After reading '1': balance = 1.
  • After reading "11": balance = 2, which exceeds 1.

Thus, the transformation fails and the answer is "NO" for that test case.

inputFormat

The input is read from stdin and consists of multiple lines. The first line contains an integer \( T \) representing the number of test cases. Each of the following \( T \) lines contains a binary string. For each test case, process the provided binary string separately.

outputFormat

For each test case, output a single line to stdout containing "YES" if the binary string can be transformed to a single character (i.e. the running balance never goes outside the range \( [-1, 1] \)), otherwise output "NO".

## sample
5
01
10
1100
1010
111000
YES

YES NO YES NO

</p>