#C9911. Longest Substring with At Most K Distinct Characters

    ID: 54057 Type: Default 1000ms 256MiB

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 determine the length of the longest substring that contains at most K distinct characters. A substring is defined as a contiguous sequence of characters.

For example, if \( s = \text{eceba} \) and \( K = 2 \), the longest substring with at most 2 distinct characters is \( \text{ece} \), which has a length of 3.

Note that the string may be empty and \( K \) can be zero. In such cases, the answer is 0.

Your solution should read input from stdin and output the result to stdout.

inputFormat

The input consists of two lines:

  • The first line contains the string \( s \). This string may be empty.
  • The second line contains an integer \( K \) representing the maximum number of distinct characters allowed in the substring.

outputFormat

Output a single integer that represents the length of the longest substring of \( s \) that contains at most \( K \) distinct characters.

## sample
eceba
2
3