DIY: Longest Substring with At Most K Distinct Characters

Solve the interview question "Longest Substring with At Most K Distinct Character" yourself in this lesson.

We'll cover the following

Problem statement

Given a string, find the length of the longest substring T that contains at most k distinct characters.

Input

The input will contain a string s and an integer k. The following is an example input:

"cdaba", 3

Output

The output will be an integer representing the length of the longest substring. The following is an example output:

4

The substring daba is the longest string in the above input that has 3 distinct characters.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.