give optimum cpp answer for the following question : Palindrome Love Chef loves…
- User
give optimum cpp answer for the following question : Palindrome Love Chef loves palindromes! A palindrome is a string that reads the same forward and backward. For example, "racecar" "racecar" and "aibohphobia" "aibohphobia" are examples of palindromes, while "fly" "fly" and "palindrome" "palindrome" are not.
Chef has with him a string S S of length N N, containing only lowercase English letters. He wants to turn S S into a palindrome. To do that, he can perform the following operation:
Choose a substring of S S that's not a palindrome and doesn't equal the full string, and delete this substring from S S. The remaining characters don't change their order. Is it possible for Chef to turn S S into a palindrome by using several (possibly, zero) of these operations? If it is, also find the minimum number of operations necessary.
Input Format The first line of input will contain a single integer T T, denoting the number of test cases. Each test case consists of two lines of input. The first line of each test case contains a single integer N N — the length of the string S S. The second line contains a string S S of length N N, containing only lowercase English letters. Output Format For each test case, print the answer on a new line:
If it's not possible to turn S S into a palindrome no matter what, print the integer − 1 −1. Otherwise, print the minimum number of operations Chef needs. Constraints 1 ≤ T ≤ 1 0 5 1≤T≤10 5
1 ≤ N ≤ 2 ⋅ 1 0 5 1≤N≤2⋅10 5
S i S i is one of the characters 'a', 'b', 'c', ..., 'z'. The sum of N N over all test cases won't exceed 2 ⋅ 1 0 5 2⋅10 5 . Sample 1: Input Output 3 7 racecar 2 my 5 modem 0 -1 1 Explanation: Test case 1 1: The string is already a palindrome, no operations are necessary.
Test case 2 2: The string is not a palindrome, but Chef also has no valid moves - there's no substring that's both not a palindrome and also not equal to the full string. So, it's impossible to change the string, and the answer is − 1 −1.
Test case 3 3: Chef can delete the substring od od comprising indices 2 2 and 3 3. This turns the string into "mem" "mem", which is a palindrome.
- Assistant