#K76132. Longest Substring with At Most K Distinct Characters

    ID: 34575 Type: Default 1000ms 256MiB

Longest Substring with At Most K Distinct Characters

Longest Substring with At Most K Distinct Characters

Given an integer \(k\) and a string \(s\) consisting of lowercase Latin letters, your task is to find the length of the longest substring that contains at most \(k\) distinct characters.

Explanation: A substring is a contiguous sequence of characters within the string. For example, if \(s = \texttt{eceba}\) and \(k = 2\), the longest substring with at most 2 distinct characters is \(\texttt{ece}\) whose length is 3.

Note: If \(k = 0\) or the string is empty, the result should be 0.

inputFormat

The input consists of two lines:

  1. The first line contains a non-negative integer \(k\), which represents the maximum number of distinct characters allowed in the substring.
  2. The second line contains the string \(s\) consisting of lowercase Latin letters. It may be empty.

outputFormat

Output a single integer, which is the length of the longest substring of \(s\) that contains at most \(k\) distinct characters.

## sample
2
eceba
3

</p>