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

LeetCode #159 - 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 requires an understanding of the sliding window technique to efficiently find the maximum length of the substring under the specified constraint.
Function Signature: def length_of_longest_substring_two_distinct(s: str) -> int:
Example 1:
Input: s = "eceba"
Output: 3
Explanation: The substring is "ece" which contains two distinct characters.
Example 2:
Input: s = "aa"
Output: 2
Explanation: The substring is "aa" which contains one distinct character.
Constraints:
  • 1 <= s.length <= 10^5

  • s consists of English letters, digits, symbols, and spaces.
Structured Response
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

Design a URL Shortener Service
System Design · 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