Dominant Character

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≤t≤105)  — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (2≤n≤106)  — 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 106.

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.

Example

input

Copy

3
2
aa
5
cbabb
8
cacabccc

output

Copy

2
-1
3

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.

个人思想:当看过题的第一反应是第 i 个往后推几个(当时想的是4个),还有一个想法浮现,用st数组存出现的次数。为什么这两个第一反应的想法没有连上,而去用s[i+1],s[i+2]……来代替,导致样例都没过

思路:当有7个时也是成立的,即abbacca 或 accabba 。读每一位时让它往后多读7位,判断是不是a多vector<int>f(3,0)的意思是,创建三个vector 值都为0 ,它是二维的,静态的。0的意思是长度为0.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>

using namespace std;

//int st[5];

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        string s;
        cin>>n>>s;

        int ans=1e9;
        for(int i=0;i<n;i++)
        {
            //vector<int>st(3,0);
            int st[3]={0};
            st[s[i]-'a']++;
            for(int j=i+1;j<min(n,i+7);j++)
            {
                st[s[j]-'a']++;
                if(st[0]>st[1]&&st[0]>st[2])
                {
                    ans=min(ans,j-i+1);
                }
            }
        }
        if(ans==1e9)
        {
            ans=-1;
        }

        cout<<ans<<endl;

    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值