Timer is Running

Maximum weight transformation of a given string
Submissions: 220   Accuracy:


  Difficulty: Easy   Marks: 2

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 



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'.


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


1<= T <=100

1<= N <=1000



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

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

Contributor: Sujnesh Mishra
Author: sujnesh

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

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

to report an issue on this page.