Grandma Capa Knits a Scarf

Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s of length n.

Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string s.

She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string s a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.

A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.

Input

The first line contains a single integer t (1≤t≤100) — the number of test cases. The next 2⋅t lines contain the description of test cases. The description of each test case consists of two lines.

The first line of each test case contains a single integer n (1≤n≤105) — the length of the string.

The second line of each test case contains the string s consisting of n lowercase English letters.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and −1, if it is impossible.

Example

input

Copy

5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky

output

Copy

2
-1
0
3
2

Note

In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.

In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.

In the third test case, you don't have to erase any symbols because the string is already a palindrome.

ps:本人真的讨厌字符串的处理,回文也是,这里只能一点一点总结规律吧,本题解析目光比较短浅。

思路:题中说只能变一个字母(直接外层循环26次,遍历每个字母),同时是回文,所以用两个指针左右同时扫描。

当l 与 r 相等时让两指针都向中间移

当l 与c(选中的字母即要去掉的)相等时,只让l向中间移

当r 与c 相等时,只让r 向中间移

当 l 和 r 不相等并且都不为c,因为只能去一种字母,所以将无法继续进行(这里的情况还需补充)

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N=1e5+10;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        string a;
        cin>>n>>a;
        int ans=n+1;
        for(int i=0;i<26;i++)
        {
            int cnt=0,l=0,r=n-1;
            while(l<=r)
            {
                if(a[l]==a[r])
                {
                    l++,r--;
                }
                else if(a[l]=='a'+i)
                {
                    cnt++;
                    l++;
                }
                else if(a[r]=='a'+i)
                {
                    cnt++;
                    r--;
                }
                else
                {
                    cnt=n+1;
                    break;
                }
            }
            ans=min(ans,cnt);
        }
        if(ans==n+1)cout<<"-1"<<endl;
        else cout<<ans<<endl;

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值