#C12229. 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.
If k
is negative, the program should output the error message: k must be a non-negative integer
.
For example, when s = "eceba"
and k = 2
, one optimal substring is "ece" with a length of 3.
inputFormat
The input consists of two lines:
- The first line is a string
s
(which may be empty). - The second line is an integer
k
.
outputFormat
Output a single line representing the length of the longest substring of s
containing at most k
distinct characters. If k
is negative, output k must be a non-negative integer
.
eceba
2
3