Dsa
Sliding Window
Medium
Longest Substring with K Distinct Characters
Given a string s and an integer k, return the length of the longest substring that contains at most k distinct characters. If k is 0, return 0.
Input:
- A single line containing a string s (1 <= |s| <= 100,000) and an integer k (0 <= k <= 26).
Output:
- Print the length of the longest substring with at most k distinct characters.
Example:
Input:
aabbcc 2
Output:
4
Key concepts
sliding_windowstringsubstring
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now