10340. Longest Substring with At Most K Distinct Characters

You are given a string. Find the length of the longest substring that contains no more than k unique characters.

Examples

Example 1:
Input: word = "abbcde", k = 2
Output: 3
Explanation: The substring "abb" has length 3 and contains at most 2 distinct characters.

Example 2:
Input: word = "bbbb", k = 1
Output: 4
Explanation: The entire string "bbbb" has only 1 unique character.

Constraints

  • 1 ≤ word.length ≤ 12,000
  • 0 ≤ k ≤ 12
  • word consists only of lowercase English letters




Please use Laptop/Desktop or any other large screen to add/edit code.