#C5337. Max Operations on AB String
Max Operations on AB String
Max Operations on AB String
You are given a string s consisting only of the characters A
and B
. Your task is to determine the maximum number of operations you can perform on the string, where each operation is defined as removing a contiguous subsegment in which the number of A
s is equal to the number of B
s.
Formally, when iterating over the characters of the string from left to right, every time the count of A
s is equal to the count of B
s, one operation can be counted and these counts are reset for the next segment. This continues until the end of the string.
For example, consider the string AABBAB
. We can perform 2 operations because there are two segments where the counts of A
and B
become equal.
Note: Use LaTeX for any mathematical formula representations if needed. In this problem, one could express the condition for an operation as when $$count_A = count_B.$$
inputFormat
The input consists of a single line containing a string s
that is composed exclusively of the characters A
and B
.
You should read the input from stdin.
outputFormat
Output a single integer to stdout indicating the maximum number of operations that can be performed on the input string.
## sampleAABBAB
2