#C7202. Longest Substring with At Most Two Distinct Characters

    ID: 51048 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

Given a string s, your task is to determine the length of the longest substring that contains at most two distinct characters. This problem tests your understanding of the sliding window technique.

For example, if s = "eceba", the longest valid substring is "ece" which has a length of 3. Similarly, for s = "ccaabbb", the longest valid substring is "aabbb" with a length of 5.

More formally, given a string s, find the maximum value of l such that there exists a substring of s with length l that contains at most two distinct characters. The problem can be formally defined using the sliding window algorithm, where you maintain a window [\(i, j\)] such that the number of distinct characters in this window is \(\leq 2\).

inputFormat

The input consists of a single line containing the string s. Note that the string can be empty.

outputFormat

Output a single integer representing the length of the longest substring of s that contains at most two distinct characters.## sample

eceba
3