#P3590. [POI 2015 R2] 三座塔 Three towers

[POI 2015 R2] 三座塔 Three towers

Description

Given a string of length nn containing only the characters B\texttt B, C\texttt C, and S\texttt S, find the longest contiguous substring such that it either contains only one kind of character, or it contains multiple kinds of characters but no two characters occur the same number of times.

Input Format

The first line contains a positive integer nn (1n1061 \leq n \leq 10^6), the length of the string. The second line contains a string of length nn.

Output Format

Output a single positive integer: the length of the longest substring that satisfies the condition.

9
CBBSSBCSC
6

Hint

Sample Explanation

Choose the substring BSSBCS\texttt {BSSBCS}.


Original title: Trzy wieże.

Translated by ChatGPT 5