Given a string s consisting only of the characters 'A' and 'B', you can perform the following operation any number of times: choose any occurrence of the substring "AB" or "BB" and delete it from s. Deleting a substring causes the characters on either side of it to become adjacent, which may create new deletable substrings.
Return the minimum possible length of s after performing the operation any number of times. Performing zero operations is allowed.
Function Signature
def min_length(s: str) -> int:
Constraints
-
1 <= len(s) <= 10^5
-
s
consists only of the characters
'A'
and
'B'
.
Examples
Input: s = "ABBA"
Output: 2
Delete "BB" to get "AA", which contains no deletable substring. Every operation removes two characters, so the length stays even, and the last 'A' can never be deleted because no 'B' comes after it, so 2 is the best possible.
Input: s = "AB"
Output: 0
Delete "AB" and the string is empty.
Input: s = "AABBB"
Output: 1
Delete the "AB" in the middle to get "ABB", then delete "AB" again to get "B". The original length is odd and each operation removes two characters, so the result cannot reach 0.