Magicsheet logo

Longest Substring with At Most Two Distinct Characters

Medium
52.5%
Updated 6/1/2025

Longest Substring with At Most Two Distinct Characters

What is this problem about?

This problem is a specific, restricted version of the "Longest Substring with At Most K Distinct Characters" problem, where K is explicitly set to 2. Given a string s, you must find the length of the longest contiguous substring that contains no more than two unique characters. For example, given "ccaabbb", the longest substring with at most two distinct characters is "aabbb", which has a length of 5.

Why is this asked in interviews?

Because K is a fixed, small constant (2), interviewers use this to see if candidates can write highly optimized, lightweight code. While the generic Hash Map solution works perfectly, using a full-blown Hash Map for exactly two characters is sometimes seen as overkill. It tests whether a candidate can manage states using simple variables or an array of size 128 (for ASCII), demonstrating an understanding of spatial efficiency.

Algorithmic pattern used

The foundational pattern is the Sliding Window. You expand a window by moving a right pointer and tracking character frequencies. When you encounter a third distinct character, your window becomes invalid. You then advance a left pointer to shrink the window until one of the two original distinct characters is completely removed from the current window's scope, freeing up a "slot" for the new third character.

Example explanation

String "ccaabbb". We want max 2 distinct characters.

  • Start at index 0: "c". 1 distinct. Valid.
  • Index 1: "cc". 1 distinct. Valid. Length = 2.
  • Index 2: "cca". 2 distinct ('c', 'a'). Valid. Length = 3.
  • Index 3: "ccaa". 2 distinct. Valid. Length = 4.
  • Index 4: "ccaab". 3 distinct ('c', 'a', 'b'). Invalid! We must shrink from the left until one character is gone. We move left past both 'c's.
  • Now window is "aab". 2 distinct ('a', 'b'). Valid.
  • Index 5: "aabb". Valid.
  • Index 6: "aabbb". Valid. Length = 5. Maximum length found is 5.

Common mistakes candidates make

A common error is resetting the left pointer completely to the current right pointer when a third character is found. This is incorrect because the end of the previous valid sequence (e.g., the "aa" before the "b") is still perfectly valid to use with the new character! You must slide the left pointer incrementally, tracking frequencies, to preserve as much of the existing valid substring as possible.

Interview preparation tip

For the Longest Substring with At Most Two Distinct Characters interview question, try solving it without a Dictionary/Map. Since you only need to track a maximum of 3 characters at any time, maintaining an array of size 128 (representing ASCII characters) and a counter for distinct_count is incredibly fast. This optimization shows deep familiarity with character encoding and array manipulation.

Similar Questions