#K67277. Longest Balanced Binary String
Longest Balanced Binary String
Longest Balanced Binary String
You are given a binary string B
consisting of characters '0' and '1'. Your task is to determine the length of the longest balanced binary string that can be obtained by removing a minimum number of characters from B
.
A binary string is said to be balanced if it contains an equal number of '0's and '1's. Mathematically, let \(n_0\) be the count of '0's and \(n_1\) be the count of '1's in the string, then the maximum length of the balanced string can be expressed as:
[ L = 2 \times \min(n_0, n_1) ]
Your program should read input from stdin and output the result to stdout.
inputFormat
The first line contains an integer T
representing the number of test cases. Each of the following T
lines contains a binary string B
.
Constraints:
- 1 ≤
T
≤ 105 - The length of each binary string is at least 1 and can be quite large.
outputFormat
For each test case, output a single line containing an integer — the length of the longest balanced binary string that can be obtained from the given input string.
## sample5
110100110
111000
0000
1
101010
8
6
0
0
6
</p>