#C9162. Reduce String
Reduce String
Reduce String
You are given a string \( s \) consisting of lowercase English letters. Your task is to repeatedly remove any two adjacent equal characters until no such pair exists. Formally, while there exists an index \( i \) such that \( s_i = s_{i+1} \), remove both characters and continue the process. In the end, output the length of the resulting string.
For example, for \( s = \texttt{abccba} \), the process is as follows:
- \( abccba \) \( \rightarrow \) remove \( cc \) to get \( abba \)
- \( abba \) \( \rightarrow \) remove \( bb \) to get \( aa \)
- \( aa \) \( \rightarrow \) remove \( aa \) to get an empty string
So, the answer is 0.
inputFormat
The input begins with an integer \( t \) specifying the number of test cases. Each of the next \( t \) lines contains a non-empty string \( s \) consisting only of lowercase English letters.
Input Format:
t s_1 s_2 ... s_t
outputFormat
For each test case, output a single line containing the length of the reduced string after performing the given operations.
Output Format:
result_1 result_2 ... result_t## sample
3
abccba
aabb
abc
0
0
3
</p>