Longest Substring with At Most K Distinct Characters

Problem Find the length of the longest substring containing at most k distinct characters.

Input / Output

  • Input: string s, int k.
  • Output: max length.

Constraints

  • |s| up to 10^5; O(n) expected.

Example

  • s = "eceba", k = 2 → 3 ("ece"); s = "aa", k = 1 → 2.
asked …
LeaderboardSalaryAccount