BETA

Difficulty Level: Easy

Submissions: 42 Accuracy:

26.19%

Maximum weight transformation of a given string
     

Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation.

Weight of a sting is calculated using below formula.

Weight of string = Weight of total pairs + 
                   weight of single characters - 
                   Total number of toggles.

Two consecutive characters are considered as pair only if they 
are different. 
Weight of a single pair (both character are different) = 4
Weight of a single character = 1 

Input:

 

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the length of string.

The second line of each test case contains the string containing only 'A' and 'B'.


Output:

Print the weight of the maximum weight transformation for each test case in a new line.


Constraints:

1<= T <=100

1<= N <=1000


Example:

Input:

Input: str = "AA"
Output: 3
Transformations of given string are "AA", "AB", "BA" and "BB". 
Maximum weight transformation is "AB" or "BA".  And weight
is "One Pair - One Toggle" = 4-1 = 3.

Input: str = "ABB"
Output: 5
Transformations are "ABB", "ABA", "AAB", "AAA", "BBB", 
"BBA", "BAB" and "BAA"
Maximum weight is of original string 4+1 (One Pair + 1
character)

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sujnesh Mishra

Set Default Code

					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.