#K61562. Minimizing Operations for Alternating String
Minimizing Operations for Alternating String
Minimizing Operations for Alternating String
You are given a string S consisting only of the characters 'A' and 'B'. In one operation, you can change any character to the other letter. Your task is to transform S into an alternating string, i.e. a string of the form ABABAB... or BABABA....
Determine the minimum number of operations needed to achieve this transformation. Note that a string with a single character is considered alternating.
Mathematical Formulation:
Let \(P_1\) be the alternating string starting with 'A' and \(P_2\) starting with 'B'. Then the answer is given by: $$\min\left(\sum_{i=0}^{N-1}\mathbf{1}_{s[i]\neq P_1[i]},\sum_{i=0}^{N-1}\mathbf{1}_{s[i]\neq P_2[i]}\right)$$ where \(\mathbf{1}_{condition}\) is 1 if the condition holds, and 0 otherwise.
inputFormat
The input begins with an integer T denoting the number of test cases. Each test case is described in a single line that contains an integer N and a string S, where N is the length of the string and S consists only of the characters 'A' and 'B'.
outputFormat
For each test case, output a single integer on a new line representing the minimum number of operations required to convert the string into an alternating string.
## sample2
3 ABA
5 AAABB
0
2
</p>