#TP0000011. Khăn choàng của bà Capa (Grandma Capa Knits a Scarf)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Khăn choàng của bà Capa (Grandma Capa Knits a Scarf)
Grandma Capa Knits a Scarf
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Grandma Capa wants to knit a scarf using a pattern written by Grandpa Sher. The pattern is a string of lowercase English letters. She considers a pattern beautiful only if it is a palindrome, meaning that it reads the same in both directions.
To modify the pattern, she may choose one letter from a to z and delete some of its occurrences. Deleting none or all of those occurrences is allowed, but deleting any other letter is forbidden. Find the minimum number of deletions needed to obtain a palindrome, or print -1 if that is impossible.
Input
- The first line contains an integer , the length of the original pattern.
- The second line contains a string of exactly lowercase English letters.
Output
Print one integer: the minimum number of deletions needed to obtain a palindrome, or -1 if it is impossible.
Subtasks
- Subtask 1 (20 points): ; ; every character of is a lowercase English letter
a–z. - Subtask 2 (30 points): ; ; every character of is a lowercase English letter
a–z. - Subtask 3 (50 points): ; ; every character of is a lowercase English letter
a–z.
Examples
Input
8
abcaacab
Output
2
Explanation
Choose letter a and delete its first and last occurrences. The result bcaacb is a palindrome. Deleting just one symbol cannot resolve the required mismatches.