#K49732. Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Given a string s, your task is to compute the length of the longest substring that contains at most two distinct characters. In other words, find the maximum length L such that there exists a substring s[i..j] with at most two unique characters, and j - i + 1 = L.
Note: If the input string is empty, the answer is 0.
The method you choose should be efficient (for instance, using a sliding window approach, which runs in O(n) time).
The mathematical condition for a valid substring can be expressed in \( \LaTeX \) as follows:
[ |{ s[k] : i \le k \le j }| \le 2 ]
inputFormat
The input consists of a single line containing the string s. This string may be empty.
outputFormat
Output a single integer representing the length of the longest substring containing at most two distinct characters.
## sampleeceba
3