P3590 [POI 2015 R2] Three Towers

Description

Given a string of length $n$ containing only the characters $\texttt B$, $\texttt C$, and $\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 $n$ ($1 \leq n \leq 10^6$), the length of the string. The second line contains a string of length $n$.

Output Format

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

Explanation/Hint

**Sample Explanation** Choose the substring $\texttt {BSSBCS}$. ---- Original title: Trzy wieże. Translated by ChatGPT 5