340. Longest Substring with At Most K Distinct Characters

Practice Longest Substring with At Most K Distinct Characters for free on NudgeCode: Medium, Sliding Window pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Given a string s and an integer k, return the length of the longest contiguous substring of s that uses at most k different characters. A character that appears several times in the substring counts once. If k is 0, the answer is 0.

Example

s = "eceba", k = 2  ->  3   ("ece")
s = "aa", k = 1     ->  2

Constraints