209. Longest Substring with At Most Two Distinct Characters

EasyStringString

Given a string s, return the length of the longest substring that contains at most two distinct characters.

Input: A single string s.

Output: An integer — the length of the longest valid substring.

Examples

Example 1
Input: eceba
Output: 3
Explanation: The longest substring with at most 2 distinct chars is 'ece' (length 3).
Example 2
Input: ccaabbb
Output: 5
Explanation: 'aabbb' has 2 distinct chars and length 5.
Example 3
Input: abc
Output: 2
Explanation: 'ab' or 'bc' both have length 2.

Constraints

Asked by

MetaGoogleMicrosoftAmazon
Solve this problem in the editor →