#C2834. Longest Substring with Two Distinct Characters

    ID: 46194 Type: Default 1000ms 256MiB

Longest Substring with Two Distinct Characters

Longest Substring with Two Distinct Characters

You are given a string and your task is to determine the length of the longest substring that contains at most two distinct characters. A substring is defined as a contiguous block of characters in the string. For example, given the string "abcbbbbcccbdddadacb", one valid substring is "bcbbbbccc" with a length of 10.

Your program should be able to process multiple queries. Each query consists of a string, and for each, you need to output the length of the longest substring satisfying the condition.

The mathematical idea behind the solution relies on the sliding window technique. Conceptually, if we let \( n \) be the length of the string, then the time complexity of the optimal solution is \( O(n) \).

inputFormat

The input begins with an integer \( T \) denoting the number of test cases. Each of the following \( T \) lines contains a non-empty string.

outputFormat

For each test case, output the length of the longest substring that contains at most two distinct characters. Each result should be printed on a new line.

## sample
2
abcbbbbcccbdddadacb
eceba
10

3

</p>