#C13052. Longest Substring with K Distinct Characters

    ID: 42548 Type: Default 1000ms 256MiB

Longest Substring with K Distinct Characters

Longest Substring with K Distinct Characters

Given a string (S) and an integer (K), find the length of the longest substring of (S) that contains at most (K) distinct characters.

For example, if (S = \texttt{eceba}) and (K = 2), the longest substring is (\texttt{ece}) with a length of 3. If (K = 0), no characters are allowed, so the answer is 0. Use a sliding window technique to achieve an efficient solution.

inputFormat

The input is given via standard input (stdin). The first line contains the string (S). The second line contains the integer (K).

outputFormat

Output a single integer to standard output (stdout), which is the length of the longest substring of (S) that contains at most (K) distinct characters.## sample

eceba
2
3

</p>