#C8216. Shortest Possible String Length
Shortest Possible String Length
Shortest Possible String Length
You are given a string s. The string may contain lowercase letters, but only the letters a and b can be removed using the following operation:
Select any non-empty contiguous substring of s that contains an equal number of a's and b's and remove it from the string. This operation can be performed any number of times (including zero). Your task is to determine the length of the shortest possible string that can be obtained by applying these operations.
Note: The minimal length can be computed by removing as many a's and b's as possible in pairs. In other words, if the number of a's is na and the number of b's is nb, then the final length is given by
\( |s| - 2 \times \min(n_a, n_b) \)
Other characters (if any) remain unaffected.
inputFormat
The input is read from standard input (stdin) as follows:
- The first line contains an integer T representing the number of test cases.
- Each of the following T lines contains a string s.
outputFormat
For each test case, output an integer on a new line representing the length of the shortest possible string after performing the allowed removal operations optimally.
## sample5
abba
abab
abcba
aaaa
bbaa
0
0
1
4
0
</p>