Find the minimum cost to turn a string into a palindrome by moving a cursor and changing characters near a chosen position.
You are given a lowercase string and a starting cursor position. In one move, you may move the cursor left or right by one position, and moving one step costs 1. You may also change a character at the current cursor position to any other lowercase letter, and changing a character costs the minimum number of circular alphabet steps between the original and new letter.
Your task is to make the whole string a palindrome with minimum total cost. You can choose the order in which positions are visited, but every necessary character change must be performed while the cursor is on that position or its mirrored counterpart, depending on your chosen strategy.
Return the minimum possible total cost.
Input Format
- A string of length consisting of lowercase English letters.
- An integer indicating the 1-indexed starting cursor position.
The exact source problem uses a single test case.
Output Format
- Print one integer: the minimum total cost to transform into a palindrome.
Constraints
- .
- .
- contains only lowercase English letters.
- The string length is moderate enough for an or solution in the intended setting.
Example 1
Input
abca 1
Output
2
Explanation
The string is already almost a palindrome; changing one of the middle characters can make it a palindrome with cost 1, and the cursor movement can be arranged with minimal additional cost in this small example. (Illustrative example.)
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.