#C11505. Shortest Substring with At Least K Distinct Characters

    ID: 40829 Type: Default 1000ms 256MiB

Shortest Substring with At Least K Distinct Characters

Shortest Substring with At Least K Distinct Characters

You are given an integer k and a string s. Your task is to find the length of the shortest substring of s that contains at least k distinct characters.

If no such substring exists, output -1.

In mathematical notation, let S be the input string and let sub be a substring of S. Find the minimum length of sub such that:

{c:csub}k|\{ c : c \in sub \}| \ge k

where \(|\{ c : c \in sub \}|\) denotes the number of distinct characters in sub.

Input/Output: The program will read input from stdin and write output to stdout.

inputFormat

The first line contains an integer T denoting the number of test cases.

For each test case, the first line contains an integer k, and the second line contains the string s.

It is guaranteed that the string s is non-empty and consists of lowercase English letters.

outputFormat

For each test case, output a single line representing the length of the shortest substring of s that contains at least k distinct characters. If such substring does not exist, output -1.

## sample
2
3
aabcabc
2
aabbcc
3

2

</p>