Grandma Capa Knits a Scarf
Contest: Codeforces Round 750 (Div. 2)
Difficulty rating: 1200
Tags: brute force, data structures, greedy, strings, two pointers

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 format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$2 \cdot 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 \le n \le 10^5$$$) — 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 \cdot 10^5$$$.

Output format:
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.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

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.

Example input:
5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky

Example output:
2
-1
0
3
2

Editorial:
Let's iterate over the letter that we will erase from the string (from 'a' to 'z'), and for each letter independently find the minimal number of erased symbols required to make the string a palindrome.
Let's say we are currently considering a letter $$$c$$$. Let's use the two pointers method: we will maintain two pointers $$$l$$$, $$$r$$$, initially $$$l$$$ points at the beginning of the string, and $$$r$$$ points at the end of the string. Now we will form a palindrome: each time we will compare $$$s_l$$$ and $$$s_r$$$, if they are equal, then we can add both of them to the palindrome at corresponding positions and iterate to symbols $$$l+1$$$ and $$$r-1$$$. If $$$s_l \neq s_r$$$, then we need to erase one of these symbols (otherwise, we won't get a palindrome), if $$$s_l=c$$$, let's erase it (we'll add to the number of erased symbols $$$1$$$ and iterate to $$$l+1$$$-th symbol), similarly, if $$$s_r=c$$$, we'll add to the number of the erased symbols $$$1$$$ and iterate to $$$r-1$$$-th symbol. And the last case, if $$$s_l \neq c$$$ and $$$s_r \neq c$$$, then it's impossible to get a palindrome from $$$s$$$ by erasing only letters equal to $$$c$$$.
The asymptotic behaviour of this solution is $$$O(|C| \cdot n)$$$, where $$$|C|$$$ is the size of the alphabet, i.e. $$$26$$$.