#K52107. Balanced String by Removing One Character
Balanced String by Removing One Character
Balanced String by Removing One Character
You are given a string consisting only of characters 'A' and 'B'. Your task is to determine whether it is possible to make the string balanced by removing exactly one character.
A string is considered balanced if the number of 'A's is equal to the number of 'B's.
In mathematical terms, let \(a\) be the number of 'A's and \(b\) be the number of 'B's in the string. The string is balanced if \(a = b\). You are allowed to remove exactly one character. Determine if by doing so, you can achieve \(a = b\).
Examples:
- For the string "ABBA": Removing any one character cannot yield a balanced string, so the answer is
NO
. - For the string "AAB": Removing one of the 'A's will yield "AB", which is balanced, hence the answer is
YES
.
You need to process multiple test cases.
inputFormat
The first line of input contains an integer \(T\) which denotes the number of test cases.
Each of the following \(T\) lines contains a single string composed solely of the characters 'A' and 'B'.
Input is provided via stdin.
outputFormat
For each test case, print a single line containing either YES
or NO
. The output for each test case should be printed on its own line via stdout.
3
ABBA
AAB
AA
NO
YES
NO
</p>