#K52252. Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Given a string s
and an integer k
, your task is to find the length of the longest substring that contains at most k
distinct characters.
For example, if k = 2
and s = "abaccc"
, the longest valid substring is "abac" with length 4.
The input is given via standard input (stdin) where the first line contains the integer k
and the second line contains the string s
. Output the result via standard output (stdout).
inputFormat
The input consists of two lines. The first line contains an integer k, the maximum number of distinct characters allowed in the substring. The second line contains the string s.
outputFormat
Output a single integer representing the length of the longest substring of s that contains at most k distinct characters.## sample
2
abaccc
4