| Codeforces Round 1054 (Div. 3) |
|---|
| Finished |
Given a string $$$s$$$ of length $$$n$$$, consisting only of the characters 'a' and 'b'.
In one operation, you can choose a position $$$i$$$ ($$$1 \le i \le n-1$$$) and swap the neighboring characters $$$s_i$$$ and $$$s_{i+1}$$$.
You need to perform the minimum number of operations to ensure that all characters of one type (either $$$a$$$ or $$$b$$$) are located strictly together, forming exactly one continuous block.
Characters of the other type can be positioned either before or after this block, forming two (possibly empty) blocks.
Examples of valid final forms:
You need to find the minimum number of described operations required to achieve the specified state.
Each test consists of several test cases.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows.
The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.
The second line contains the string $$$s$$$ of length $$$n$$$, consisting only of the characters 'a' and 'b'.
It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one integer — the minimum number of operations required for all characters of one of the two types to form a single continuous block.
54abab6bababa7abababa2ab1b
12200
In the first test case, the initial string is 'abab':
In the fifth input test case, the string consists of a single character 'b'. The single character already forms a continuous block, no swaps are needed, so the minimum number of operations is $$$0$$$.
| Name |
|---|


