#K92032. Maximum Balanced Substring Length
Maximum Balanced Substring Length
Maximum Balanced Substring Length
Given a string s
consisting only of letters 'a'
and 'b'
, your task is to compute the maximum length of a balanced substring. A substring is considered balanced if it contains an equal number of 'a'
's and 'b'
's.
Note: A substring is a contiguous block of characters within the string.
Examples:
- For
s = "aabb"
, the entire string is balanced, so the answer is4
. - For
s = "aaabb"
, the longest balanced substring is "aabb" with length4
. - For
s = "aaaa"
ors = "bbbb"
, no balanced substring exists, so the answer is0
.
Your solution must read input from stdin and print the result to stdout.
inputFormat
The input consists of a single line containing a non-empty string s
composed only of the characters 'a'
and 'b'
.
Examples:
aabb
outputFormat
Output a single integer representing the maximum length of a balanced substring within the given string.
Examples:
4## sample
aaaa
0
</p>