#C8022. Longest Substring with At Most K Distinct Characters

    ID: 51959 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, find the length of the longest substring of s that contains at most k distinct characters. Formally, if we denote by \( f(s, k) \) the length of the longest substring with no more than \( k \) distinct characters, you are required to compute \( f(s, k) \).

Note:

  • If k is zero or the input string is empty, the answer should be 0.

Example:

Input: s = "araaci", k = 2
Output: 4

The substring "araa" has 2 distinct characters ('a' and 'r') and length 4.

inputFormat

The input is read from standard input. It consists of two lines:

  • The first line contains the string s.
  • The second line contains an integer k.

outputFormat

Output a single integer representing the length of the longest substring of s that contains at most k distinct characters.

## sample
araaci
2
4