codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1582C.md (2610B)


      1 Source: https://codeforces.com/problemset/problem/1582/C
      2 
      3 C. Grandma Capa Knits a Scarf
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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$.
     22 
     23 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$.
     24 
     25 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.
     26 
     27 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.
     28 
     29 Input
     30 
     31 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.
     32 
     33 The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the string.
     34 
     35 The second line of each test case contains the string $s$ consisting of $n$ lowercase English letters.
     36 
     37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     38 
     39 Output
     40 
     41 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.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 5
     49 8
     50 abcaacab
     51 6
     52 xyzxyz
     53 4
     54 abba
     55 8
     56 rprarlap
     57 10
     58 khyyhhyhky
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 2
     65 -1
     66 0
     67 3
     68 2
     69 ```
     70 
     71 Note
     72 
     73 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.
     74 
     75 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.
     76 
     77 In the third test case, you don't have to erase any symbols because the string is already a palindrome.