#K46027. Balancing A's and B's

    ID: 27885 Type: Default 1000ms 256MiB

Balancing A's and B's

Balancing A's and B's

You are given a string consisting solely of the characters A and B. Your task is to determine the minimum number of deletions required so that the number of A's equals the number of B's in the string. In other words, if the string originally contains NA occurrences of A and NB occurrences of B, you should delete exactly |NA - NB| characters from the string.

Note: The deletions need not be contiguous. You are only required to return the minimum number of deletions needed to balance the counts.

Examples:

  • For the input "AB", the output is 0 since the counts are already equal.
  • For the input "AAB", the output is 1 because one deletion (removing an extra A) balances the string.
  • For the input "AAABB", the output is 1.
  • For the input "AAAABBBB", the output is 0.
  • For the input "AAAA", the output is 4.
  • For the input "BBBB", the output is 4.

The mathematical operation employed is the absolute difference between the counts of A and B, i.e., \( |\text{count}(A) - \text{count}(B)| \).

inputFormat

The first line contains a single integer t representing the number of test cases. Each of the following t lines contains a string s composed solely of the characters A and B.

outputFormat

For each test case, print on a new line a single integer representing the minimum number of deletions required to balance the string.

## sample
1
AB
0

</p>