#C7303. Longest Substring with At Most Two Distinct Characters

    ID: 51160 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

You are given a string s. Your task is to determine the length of the longest contiguous substring of s that contains at most two distinct characters. A substring is a sequence of consecutive characters. For example, when s = "eceba", the longest valid substring is ece which has a length of 3.

Note: If the string is empty, the answer is 0.

The problem can be mathematically expressed as finding the maximum length L such that for some substring s[i...j] (with L = j - i + 1), the number of distinct characters satisfies:

{s[k]:ikj}2|\{ s[k] : i \leq k \leq j \}| \leq 2

inputFormat

The input consists of a single line containing the string s. The string may be empty and can include any ASCII characters.

outputFormat

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

## sample
eceba
3