#C2579. Longest Substring with At Most Two Distinct Characters
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 find the length of the longest contiguous substring of s that contains at most two distinct characters.
Example:
- For s = "abcbbbbcccbdddadacb", the longest such substring is "bcbbbbccc" with a length of 10.
Note: The substring must be contiguous.
Mathematical Formulation:
Let s be a string and define a substring s[i...j]. Find \[ \max_{0 \le i \le j < n} \{ (j-i+1) \; : \; |\{ s[k] : i \le k \le j \}| \le 2 \}. \]
inputFormat
The input consists of a single line containing the string s
. The string may be empty and consists of ASCII characters.
outputFormat
Output a single integer representing the length of the longest substring that contains at most two distinct characters.
## sampleabcbbbbcccbdddadacb
10