10159. Longest Substring with At Most Two Distinct Characters

Given a string text, determine the length of the longest substring that contains no more than 2 different characters.

Examples:

Example 1:
Input: "abaccc"
Output: 4
Explanation: The answer is "accc", which has length 4.
Example 2:
Input: "bbaaac"
Output: 5
Explanation: The answer is "bbaaa", which has length 5.
Example 3:
Input: "deeeeef"
Output: 6
Explanation: The answer is "eeeeef", which has length 6.

Constraints:

  • 1 <= text.length <= 20,000
  • text consists of English letters.




Please use Laptop/Desktop or any other large screen to add/edit code.