#K926. Longest Substring with Exactly N Distinct Characters

    ID: 38233 Type: Default 1000ms 256MiB

Longest Substring with Exactly N Distinct Characters

Longest Substring with Exactly N Distinct Characters

You are given a string S consisting of lowercase English letters and an integer n. Your task is to determine the length of the longest contiguous substring of S that contains exactly n distinct characters.

In other words, if you denote the substring by S[i...j], then the number of unique characters in this substring should be exactly $$n$$. If no such substring exists, output 0.

inputFormat

The input is provided via stdin and consists of two lines:

  1. The first line contains the string S (only lowercase English letters).
  2. The second line contains an integer n indicating the desired number of distinct characters.

outputFormat

Output a single integer to stdout — the length of the longest substring of S that contains exactly $$n$$ distinct characters. If no such substring exists, output 0.

## sample
abcba
2
3

</p>