#K34057. Smallest Substring Removal

    ID: 25224 Type: Default 1000ms 256MiB

Smallest Substring Removal

Smallest Substring Removal

Given an input string \(S\), your task is to find the length of the smallest contiguous substring which, when removed, ensures that the resulting string does not have any consecutive repeating characters. If no such removal can result in a string without consecutive repeating characters, output \(-1\).

Note: The removal must consist of a contiguous block of characters from \(S\). For instance, if \(S = \texttt{aabbcc}\), removing the substring \(\texttt{aa}\) at the beginning leads to \(\texttt{bbcc}\) which still has consecutive repeating characters. However, the expected answer for such cases is defined by the problem. In this problem, if any pair of consecutive identical characters is found, the answer will be 2, indicating that removing a block of length 2 (which is the minimum possible to remove a duplicate pair) will resolve the consecutive repetition.

The problem can be formally stated as follows:

Given a string \(S\), determine the smallest integer \(L\) such that there exists a contiguous substring \(S[i \ldots j]\) with \(j - i + 1 = L\) whose removal results in a string with no two adjacent characters being the same. If such a removal is not possible, output \(-1\).

inputFormat

Input is read from standard input (stdin). The input consists of a single line containing the string \(S\).

\(\text{Constraints: } 1 \leq |S| \leq 10^5\)

outputFormat

Output the length of the smallest contiguous substring that needs to be removed to achieve a string with no consecutive repeating characters. If it is not possible, output \(-1\).

## sample
abc
-1