#K61082. Longest Balanced Substring

    ID: 31230 Type: Default 1000ms 256MiB

Longest Balanced Substring

Longest Balanced Substring

Given a string (S) consisting only of the characters 'A' and 'B', determine the length of the longest contiguous substring in which the number of 'A's equals the number of 'B's. Such a substring is called (\textbf{balanced}), i.e. if (#A = #B). For example, if (S = \texttt{AABBAA}), the longest balanced substring is (\texttt{ABBA}) with length (4). If no such substring exists, output (0).

inputFormat

Input is read from standard input (stdin) and consists of a single line containing the string (S) (where (1 \leq |S| \leq 10^5)) composed only of the characters 'A' and 'B'.

outputFormat

Output a single integer to standard output (stdout) representing the length of the longest balanced substring of (S).## sample

AABBAA
4