#C6152. Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Given a string S, find the length of the longest contiguous substring that contains at most two distinct characters.
For example, for S = "eceba"
, the longest substring is "ece" with a length of 3, and for S = "ccaabbb"
, the longest valid substring is "aabbb" with a length of 5.
This problem requires you to implement an efficient sliding window algorithm with a time complexity of \(O(n)\), where \(n\) is the length of the string.
inputFormat
The input is read from standard input (stdin) and has the following format:
T S1 S2 ... ST
Here, T
(an integer) is the number of test cases, and each Si
is a string representing a single test case.
outputFormat
For each test case, output a single integer on a new line that represents the length of the longest contiguous substring of S that contains at most two distinct characters. The output should be written to standard output (stdout).
## sample2
eceba
ccaabbb
3
5
</p>