Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Coding Round 1 Senior programming

Longest Substring with At Most Two Distinct Characters

Given a string s, return the length of the longest substring that contains at most two distinct characters. This problem will test your understanding of the sliding window technique.
Function Signature: def length_of_longest_substring_two_distinct(s: str) -> int:
Example 1:
Input: s = "eceba"
Output: 3
Explanation: The longest substring is "ece" with length 3.
Example 2:
Input: s = "ccaabbb"
Output: 5
Explanation: The longest substring is "aabbb" with length 5.
Constraints:
  • 1 <= s.length <= 10^5

  • s consists of English letters.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free

More Coding Round 1 Interview Prep

LeetCode #159 - Longest Substring with At Most Two Distinct Characters
Coding Round 1 · Mid-Level
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Link copied to clipboard