#K40772. Longest Substring with At Most Two Distinct Characters

    ID: 26717 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

Given a string \( s \), determine the length of the longest contiguous substring that contains at most two distinct characters. This problem can be efficiently solved using a sliding window approach, ensuring an overall time complexity of \(O(n)\), where \(n\) is the length of the string.

For example, if \( s = \texttt{eceba} \), the longest valid substring is \( \texttt{ece} \) which has a length of 3.

inputFormat

The input is provided via standard input (stdin) and consists of a single line containing the string \( s \). The string may contain both lower-case and upper-case English letters and is guaranteed to be non-empty.

outputFormat

Output to standard output (stdout) a single integer representing the length of the longest substring of \( s \) that contains at most two distinct characters.

## sample
eceba
3