String BalanceMedium

String Balance

MediumPractice library · +20 XPstringstwo pointers

Given a string S consisting of characters 'a', 'b', and 'c', find the length of the longest substring that has an equal number of 'a's, 'b's, and 'c's. If no such substring exists, output 0.

Input

A single line containing the string S.

Output

A single integer representing the length of the longest balanced substring.

Constraints

1 <= |S| <= 200000. S contains only 'a', 'b', and 'c'.

Time limit 1 s (native code; slower languages get more)Memory 256 MB16 hidden tests

Example 1

Input
abcabcabc
Output
9

The whole string is balanced (3 'a's, 3 'b's, 3 'c's), length is 9.

Example 2

Input
abacaba
Output
3

The longest balanced substring is 'aba' (not balanced), 'bac' (not balanced), 'aca' (not balanced), 'cab' (not balanced), 'aba' (not balanced). The longest is 'abacaba' itself has 4 'a', 2 'b', 1 'c'. 'aba' at index 0: 2a, 1b. 'bac' at index 1: 1a, 1b, 1c. Length 3. 'acaba': 3a, 1b, 1c. Consider 'ab

Hints