Codeforces Round 754 (Div. 2)
C
Dominant Character
Codeforces
1400
原题
← BD →
翻译中…

Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.

翻译中…

He wants to find the length of the smallest substring, which satisfies the following conditions:

翻译中…
  • Length of the substring is at least $2$
  • 'a' occurs strictly more times in this substring than 'b'
  • 'a' occurs strictly more times in this substring than 'c'
翻译中…

Ashish is busy planning his next Codeforces round. Help him solve the problem.

翻译中…

A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ $(1 \le t \le 10^{5})$  — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$  — the length of the string $s$.

翻译中…

The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.

翻译中…

Output

翻译中…

For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.

翻译中…

Note

翻译中…

Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.

翻译中…

In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.

翻译中…

In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$.

翻译中…

样例

Input #1
3
2
aa
5
cbabb
8
cacabccc
Output #1
2
-1
3
InputCopy OutputCopy
题目信息
题号
1605C
时间限制
2 s
内存限制
256 MB
标签
暴力
贪心
模拟
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。