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)

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.

mantan_g | 267 |

tushki | 198 |

aksht7 | 182 |

ayush1101 | 128 |

namansharmacse17 | 110 |

ARUN_PUNDIR | 730 |

manishgupta8497 | 627 |

AMAN__JAIN | 625 |

Abhimanyu_kumar | 562 |

AJ786 | 541 |

blackshadows | 5335 |

Ibrahim Nash | 5223 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4584 |

Login to report an issue on this page.