/159. Longest Substring with At Most Two Distinct Characters

159. Longest Substring with At Most Two Distinct Characters

Medium
Hash Table57% acceptance

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

Example 1

Input: "abacccbaaa"

Output: 5

Explanation: The longest substring with at most two distinct characters is "ccbaa".

Example 2

Input: "xyyyz"

Output: 4

Explanation: The longest substring with at most two distinct characters is "yyyz".

Constraints

  • 1 <= len(s) <= 105
  • s consists of English letters (both lowercase and uppercase)
Python (current runtime)

Case 1

Input: "aabbccdde"

Expected: 4

Case 2

Input: "abcabcabc"

Expected: 2

Case 3

Input: "zzzzzz"

Expected: 6

Case 4

Input: "pqrstuv"

Expected: 1

Case 5

Input: "aabbaaabbb"

Expected: 7