Dsa
Sliding Window
Medium
Longest Subarray with Distinct Characters
Given a string 's', find the length of the longest substring that contains at most 'k' distinct characters. If 'k' is 0, return 0.
Input Format:
- The first line contains the string 's' (1 ≤ |s| ≤ 10^5).
- The second line contains an integer 'k' (0 ≤ k ≤ 26).
Output Format:
- Print a single integer representing the maximum length of the substring.
Example:
Input:
cbaacc
2
Output:
4
(The longest substring is 'baac' which has a length of 4.)
Key concepts
sliding_windowstringdistinct
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