#K56262. Counting Substrings with Limited Distinct Characters

    ID: 30160 Type: Default 1000ms 256MiB

Counting Substrings with Limited Distinct Characters

Counting Substrings with Limited Distinct Characters

You are given a string s and two integers k and m. Your task is to count the number of contiguous substrings of s that have length exactly k and contain at most m distinct characters.

In mathematical terms, let \( S \) be the input string, and consider all substrings \( S[i, i+k-1] \) where \( 0 \leq i \leq |S| - k \). For each substring, if the number of distinct characters \( \leq m \) then it is considered valid. Output the count of all valid substrings.

Note: The parameters satisfy \( k \leq |S| \). If \( k > |S| \), no substring exists, and the answer is 0.

inputFormat

The input is given via standard input (stdin) in the following format:

<string>
<k>
<m>

Here, <string> is a non-empty string without spaces, and <k> and <m> are integers.

outputFormat

Output a single integer to standard output (stdout) representing the count of substrings of length k that contain at most m distinct characters.

## sample
abcbaa
3
2
2

</p>