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.
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.
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.
String "ccaabbb". We want max 2 distinct characters.
"c". 1 distinct. Valid."cc". 1 distinct. Valid. Length = 2."cca". 2 distinct ('c', 'a'). Valid. Length = 3."ccaa". 2 distinct. Valid. Length = 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."aab". 2 distinct ('a', 'b'). Valid."aabb". Valid."aabbb". Valid. Length = 5.
Maximum length found is 5.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.
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.