#C1811. Longest Substring After Operation

    ID: 45058 Type: Default 1000ms 256MiB

Longest Substring After Operation

Longest Substring After Operation

You are given a string s consisting only of the characters \(a\), \(b\), and \(c\). You are allowed to perform exactly one operation: choose any character in s and change it to any character among \(a\), \(b\), and \(c\). Note that the operation is mandatory; in cases where the string already consists of only one type of character, you may change a character to the same value.

Your task is to determine the length of the longest contiguous substring that contains the same character after performing this operation exactly once.

If \(L(c)\) represents the maximum length of a contiguous block of character \(c\) in \(s\), then the answer is given by:

\(\text{Answer} = \min\Big\{\max_{c \in \{a, b, c\}} \big(L(c) + 1\big),\; |s|\Big\}\)

For example:

  • If \(s = \texttt{aab}\), then the answer is 3.
  • If \(s = \texttt{abc}\), then the answer is 2.
  • If \(s = \texttt{aaa}\), then the answer is 3.

inputFormat

The input begins with an integer \(T\) representing the number of test cases.

Each of the following \(T\) lines contains a non-empty string s consisting only of the characters \(a\), \(b\), and \(c\>.

outputFormat

For each test case, output a single integer denoting the length of the longest contiguous substring with identical characters that can be obtained after performing the operation exactly once.

## sample
3
aab
abc
aaa
3

2 3

</p>