#C11911. Maximum Equal Subsequence Length

    ID: 41280 Type: Default 1000ms 256MiB

Maximum Equal Subsequence Length

Maximum Equal Subsequence Length

You are given a binary string S consisting of characters '0' and '1'. Your task is to determine the length of the longest contiguous subsequence in which the number of '0's and '1's are equal.

To solve this problem, you can use a prefix sum approach. Convert each '1' to +1 and each '0' to -1. Then, the problem is equivalent to finding a subarray whose sum is 0. Formally, if you define

$$\text{count}(i) = \sum_{j=0}^{i} \left(\begin{cases} +1 & \text{if } S[j]=1\\ -1 & \text{if } S[j]=0 \end{cases}\right), $$

you need to find the maximum distance between two indices i and j (with i < j) such that

count(i)=count(j).\text{count}(i) = \text{count}(j).

The input will be provided from stdin and the output should be printed to stdout, with each result in a new line for each test case.

inputFormat

The first line of the input contains an integer T, representing the number of test cases. The following T lines each contain a single binary string S.

outputFormat

For each test case, output a single integer which is the length of the longest contiguous subsequence with an equal number of '0's and '1's. Each result should be printed on a new line.

## sample
1
1100011
6

</p>