CF BUDDY
← Problems·

1907C · Removal of Unattractive Pairs

1200 · constructive algorithms, greedy, math

Problem: Vlad found a string ss consisting of nn lowercase Latin letters, and he wants to make it as short as possible.

To do this, he can remove any pair of adjacent characters from ss any number of times, provided they are different. For example, if ss=racoon, then by removing one pair of characters he can obtain the strings coon, roon, raon, and raco, but he cannot obtain racn (because the removed letters were the same) or rcon (because the removed letters were not adjacent).

What is the minimum length Vlad can achieve by applying any number of deletions?

Input Format: The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. Descriptions of the test cases follow.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line of each test case contains the string ss consisting of nn lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single number—the minimum length of the string ss, after removing pairs of adjacent characters with different values.

Note: In the first test case of the example, you need to act as follows: "aabc" \rightarrow "ac" \rightarrow "". Note that with a different order of deletions, the string will not become empty.

Sample Cases

Case 1

Input

10
4
aabc
5
abaca
10
avbvvcvvvd
7
abcdefg
5
dabbb
8
aacebeaa
7
bbbbacc
6
dacfcc
6
fdfcdc
9
dbdcfbbdc

Output

0
1
2
1
1
0
1
0
0
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback