#C10454. Minimum Balanced Substring Length
Minimum Balanced Substring Length
Minimum Balanced Substring Length
Given a string s
and an integer k
, you are required to find the minimum length of a contiguous substring of s
that is balanced. A substring is considered balanced if each letter among the first k
lowercase English letters (i.e., from a
to the kth letter) appears the same number of times.
If no such substring exists, output -1
.
Note: A balanced substring must have a total length that is a multiple of k
. In many cases the minimum balanced substring will be one where each of the required letters appears exactly once.
inputFormat
The first line contains an integer t
denoting the number of test cases.
Each test case consists of two lines:
- The first line contains an integer
k
. - The second line contains a string
s
consisting only of lowercase English letters.
outputFormat
For each test case, output the minimum length of a balanced contiguous substring. If no balanced substring exists, output -1
. Each answer should be printed on a new line.
6
2
abba
3
abcabc
2
xyz
1
a
2
aaaaabbbbbb
26
abcdefghijklmnopqrstuvwxyz
2
3
-1
1
2
26
</p>