#K89102. Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
You are given T test cases. Each test case consists of a string s. Your task is to determine the length of the longest contiguous substring of s that contains at most two distinct characters.
For example, consider the string eceba
. The longest substring with at most 2 distinct characters is ece
with a length of 3.
Note: In the input, each test case is given in a new line after the number of test cases.
The problem can be mathematically stated as follows:
Given a string \(s\) of length \(n\) and an integer \(k=2\), find: \[ L = \max_{0 \leq i \leq j < n \text{ and } |\{s[i], s[i+1], \dots, s[j]\}| \leq 2} (j - i + 1) \]
Output the length \(L\) for each test case on a separate line.
inputFormat
The first line of input contains an integer T, the number of test cases.
Each of the following T lines contains a string s which may be empty. The string consists of lowercase English letters.
Input is read from standard input (stdin).
outputFormat
For each test case, output a single integer representing the length of the longest substring that contains at most two distinct characters. Each result should be printed on a new line to standard output (stdout).
## sample2
eceba
ccaabbb
3
5
</p>