Xenny and Classroom
Practice
4.2 (39 votes)
Ready
Ad Hoc
Easy
Problem
34% Success 7094 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Xenny was a teacher and his class consisted of N boys and N girls. He took all his students to the playground and asked them to stand in a straight line. The boys and the girls formed a perfect line, but Xenny seemed unhappy. He wanted them to form an alternating sequence of boys and girls, i.e., he wanted that after a boy, there should be a girl in the line, and vice-versa.

In order to form an alternating sequence, a boy and a girl could swap their positions.

Given the original sequence in which the boys and girls were standing, find the minimum number of swaps required to form an alternating sequence.

Note: An alternating sequence can start either with a boy or a girl.

Input

First line of input consists of a natural number T - the number of testcases.
T testcases follow. For each testcase:

First line consists of a single integer N - the number of boys, and the number of girls.
Second line consists of a string of length \(2N\), representing the original sequence.
In this string, a boy is represented by uppercase character B and a girl by G.

Output

For each testcase, print the minimum number of swaps required to get an alternating sequence on a new line.

Constraints

\(1 \le T \le 5\)

\(1 \le N \le 10^6\)

Subtasks

  1. \(1 \le N \le 20\) : 10 points
  2. \(1 \le N \le 10^6\) : 90 points

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
27 votes
Tags:
Brute-force searchMathematicsApprovedEasy
Points:50
4 votes
Tags:
AlgorithmsApprovedBit ManipulationFenwick treeHardOpenTrees
Points:20
6 votes
Tags:
ApprovedData StructuresEasyImplementationMerge sortSimulationSortingapproved