#C5610. Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
You are given a string s
and an integer k
. Your task is to find the length of the longest substring of s
that contains at most k
distinct characters.
More formally, let a substring s[i...j]
be valid if it contains at most k distinct characters. You need to compute the value:
\(\max_{0 \leq i \leq j < n}\{j - i + 1 \mid s[i...j] \text{ contains at most } k \text{ distinct characters}\}\)
where n is the length of the string. It is guaranteed that the string consists only of lowercase English letters.
inputFormat
The input is given on two lines:
The first line contains a non-negative string s
consisting of lowercase alphabets.
The second line contains an integer k
denoting the maximum number of distinct characters allowed.
outputFormat
Print the length of the longest substring of s
that contains at most k
distinct characters to standard output.
eceba
2
3