#K37642. Longest Substring with At Most N Distinct Characters
Longest Substring with At Most N Distinct Characters
Longest Substring with At Most N Distinct Characters
You are given an integer n
and a string s
. Your task is to find the length of the longest substring of s
that contains at most n
distinct characters.
In other words, if a substring contains more than n
unique characters, it is not valid. The answer is the maximum possible length of a valid substring.
Mathematically, if we denote by \(f(s, n)\) the length of the longest substring with at most n
distinct characters, you need to compute \(f(s, n)\).
Note: If n
is 0, or the string is empty, the result is 0.
inputFormat
The input is given via stdin and consists of two lines. The first line contains an integer n
. The second line contains the string s
.
For example:
2 eceba
outputFormat
Output a single integer representing the length of the longest substring containing at most n
distinct characters. The output should be written to stdout.
2
eceba
3