ab string

0

0 votes
Dynamic Programming
Problem

 A string is called as ab string if it is in the form of aibjak  or biakbk  where i>=0,j>=0,k>=0 .

Now you are given a string consisting of only 'a' and 'b' in it.You are allowed to remove any number of characters from this string (possibly 0). You need to find the maximum possible size of ab string can be obtained.

Input :

  • First line of input contains an integer T dentoing no of test cases.
  • Next T lines will be given a string.

Output :

For each test case output maximum possible size of ab string that can be obtained in a new line.

Constraints :

1<=T<=100

1<=|S|<=106

 

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In first test case, you can remove 2nd,5th,14th,18th characters to get maximum possible size of 14.

In second test case, you no need to remove anything, so maximum size is 3.

In third test case,you remove 6th character to get string of size 8.

Editor Image

?