250. Longest Substring with At Most K Distinct Characters

MediumStringString

Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters. Input: '"s", k'.

Input: '"s", k'.

Output: Integer — length of longest valid substring.

Examples

Example 1
Input: "eceba", 2
Output: 3
Explanation: 'ece' has 2 distinct chars.
Example 2
Input: "aa", 1
Output: 2
Explanation: 'aa' has 1 distinct.
Example 3
Input: "abc", 1
Output: 1
Explanation: At most 1 distinct char.

Constraints

Asked by

OracleAppleMicrosoftAmazonGoogleMeta
Solve this problem in the editor →