P2697 Gem String

Description

There is a kind of gem string made of emeralds and rubies. The string is most stable and least likely to break only when the numbers of emeralds and rubies are equal. An An wants to know, from a given gem string, how many gems the longest stable substring contains. Please help him. Emeralds are denoted by $\texttt G$, and rubies are denoted by $\texttt R$.

Input Format

One line, a string consisting of $\texttt G$ and $\texttt R$.

Output Format

One line with an integer, the number of gems in the longest stable gem string.

Explanation/Hint

$\texttt {RGGR}$ is the answer. Constraints: the number of gems is at most $10^6$. Translated by ChatGPT 5