Hone logo
Hone
Problems

Longest Substring with At Most Two Distinct Characters

This challenge asks you to find the length of the longest substring within a given string that contains at most two distinct characters. This problem is a common interview question and demonstrates your ability to use sliding window techniques to efficiently process strings. It's useful in scenarios like data compression and pattern recognition where identifying repeating sequences is important.

Problem Description

You are given a string s consisting of lowercase English letters. Your task is to determine the length of the longest substring of s that contains at most two distinct characters.

What needs to be achieved:

Write an algorithm that iterates through the string s and identifies the longest substring that satisfies the condition of having at most two distinct characters. The algorithm should return the length of this longest substring.

Key Requirements:

  • The substring must be contiguous (i.e., consecutive characters in the original string).
  • The substring must contain at most two distinct characters.
  • If multiple substrings satisfy the condition and have the same maximum length, return that length.
  • If the input string is empty, return 0.

Expected Behavior:

The algorithm should efficiently scan the string and maintain a sliding window. The window expands as long as the number of distinct characters within it is at most two. When the number of distinct characters exceeds two, the window's left boundary is moved to shrink the window until the condition is met again. The maximum length of the window encountered during this process is the desired result.

Edge Cases to Consider:

  • Empty input string.
  • String with only one distinct character.
  • String with two distinct characters.
  • String with more than two distinct characters.
  • Strings with repeating patterns.

Examples

Example 1:

Input: "eceba"
Output: 3
Explanation: The longest substring is "ece", which has length 3 and contains only two distinct characters ('e' and 'c').

Example 2:

Input: "ccaabbb"
Output: 5
Explanation: The longest substring is "aabbb", which has length 5 and contains only two distinct characters ('a' and 'b').

Example 3:

Input: "abaccc"
Output: 4
Explanation: The longest substring is "accc", which has length 4 and contains only two distinct characters ('a' and 'c').

Example 4:

Input: ""
Output: 0
Explanation: Empty string, so the longest substring length is 0.

Constraints

  • 1 <= s.length <= 100000
  • s consists of lowercase English letters.
  • The algorithm should have a time complexity of O(n), where n is the length of the string.

Notes

Consider using a sliding window approach. A hash map (or dictionary) can be used to efficiently track the frequency of characters within the current window. Think about how to efficiently expand and contract the window while maintaining the constraint of at most two distinct characters. Don't forget to handle the edge case of an empty string.

Loading editor...
plaintext