Coding

Longest Substring with Two Distinct Characters

Find the longest substring with at most two distinct characters.

Input: s = "ccaabbb"

Output: 5

Explanation: The substring "aabbb" contains exactly two distinct characters and has a maximum length of 5.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

No answers yet

Be the first to share your approach to this question

Practice More Questions

2 answers published. Last candidate submission on Sep 7 2026, 2:02pm PDT. Interview question asked to Software Engineers interviewing at Taiwan Semiconductor, Unacademy, GameStop and other companies. Original question asked: Longest Substring with Two Distinct Characters. Question last reported by a candidate interviewing at Better.com for a Software Engineer interview on Oct 6 2026, 2:35am PDT.