#K36167. 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, if a substring starts at index i and ends at index j, then its length is given by \( j - i + 1 \). The substring is valid if it contains at most K
distinct characters.
If no valid substring exists, print 0
.
inputFormat
The input consists of two lines:
- The first line contains the string
S
. - The second line contains the integer
K
.
outputFormat
Output a single integer representing the length of the longest substring of S
that contains at most K
distinct characters.
eceba
2
3