#C5256. Longest Substring with at Most Two Distinct Characters after One Replacement

    ID: 48885 Type: Default 1000ms 256MiB

Longest Substring with at Most Two Distinct Characters after One Replacement

Longest Substring with at Most Two Distinct Characters after One Replacement

You are given a string \(S\) consisting of lowercase English letters. You are allowed to perform at most one replacement operation: choose exactly one character in \(S\) and replace it with any other lowercase letter. After this operation (or without any replacement), your task is to find the length of the longest contiguous substring that contains at most two distinct characters.

Note: It is allowed not to perform any replacement if it does not improve the result. The answer is the maximum possible length achievable under these conditions.

Input Constraints: \(S\) is a non-empty string with length up to reasonably small limits for a brute-force solution.

Examples:

Input: eceba
Output: 4

Input: ccaabbb Output: 6

Input: aaa Output: 3

</p>

In the first example, by replacing one of the characters, one optimal move can yield a substring (for example, "ecec") with length 4 that contains only 'e' and 'c'.

inputFormat

The first line contains a single integer \(T\), the number of test cases. Each of the following \(T\) lines contains a non-empty string \(S\) composed of lowercase English letters.

For example:

5
eceba
ccaabbb
aaa
abcde
aaaaa

outputFormat

For each test case, output a single integer on a new line representing the length of the longest substring of \(S\) that contains at most two distinct characters after performing at most one replacement.

For the sample input above, the output should be:

4
6
3
3
5
## sample
5
eceba
ccaabbb
aaa
abcde
aaaaa
4

6 3 3 5

</p>