#C8058. Maximum Segment Balance
Maximum Segment Balance
Maximum Segment Balance
This problem asks you to compute the maximum balance of any segment of a given string S
consisting only of the characters 'a' and 'b'. The balance of a segment is defined as the absolute difference between the number of 'a's and 'b's in that segment. Formally, if a segment contains A occurrences of 'a' and B occurrences of 'b', its balance is \( |A - B| \).
Note: Although a typical solution to such problems might require checking all segments, in this problem you are expected to compute the balance using a cumulative approach. For each character in the string, treat 'a' as +1 and 'b' as -1, and maintain the cumulative sum. The answer will be the maximum absolute value of this cumulative sum.
For example, for the input abbaaabb
the maximum balance is 2.
inputFormat
The input consists of a single line containing the string S
which only comprises the characters 'a' and 'b'.
\(1 \leq |S| \leq 10^5\)
outputFormat
Output a single integer which is the maximum balance among all segments of the string S
as computed using the cumulative sum approach.
abbaaabb
2