#K71762. Longest Substring with At Most Two Distinct Characters

    ID: 33604 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. A substring is a contiguous sequence of characters in the string. For instance, if s = "abaccc", the longest substring satisfying the condition is "accc", with a length of 4.

You are encouraged to use efficient techniques such as the sliding window method. The solution should handle edge cases, including strings with fewer than two distinct characters and empty strings.

The mathematical formulation of the problem can be stated as:

Find L where \[ L = \max_{0 \leq i \leq j < n} \{ j - i + 1 \, : \, |\{s_k : i \leq k \leq j\}| \leq 2 \}\]

inputFormat

The input consists of a single line containing the string s. The string may include letters, digits, or symbols. Note that the input could be empty.

outputFormat

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

## sample
abaccc
4