Dsa
Sliding Window
Warm-up
Longest Substring with Repeating Character Limit
Given a string and an integer k, find the length of the longest substring that contains at most k distinct characters.
For example, for the input "eceba" and k = 2, the output should be 3 because the longest substring with at most 2 distinct characters is "ece".
Input Format:
- A single line containing the string s.
- A single line containing integer k.
Output Format:
- A single integer representing the length of the longest substring with at most k distinct characters.
Example:
Input:
eceba
2
Output:
3
Key concepts
sliding_windowstringcounting
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