Given a string **S** of length **N** consisting of lowercase alphabets, the task is to find the minimum number of operations to convert the given string into a palindrome. In one operation, choose any character and replace it by its next or previous alphabet.

Note:The alphabets are cyclic i.e., ifzis incremented then it becomesaand ifais decremented then it becomesz.

**Examples:**

Input:S = “arcehesmz”Output:16Explanation:

The possible transformation that given the minimum count of operation is:

- Decrease 1st character
‘a’by 1 to get‘z’. Count of operation = 1- Decrease 3rd character
‘c’by 10 to get‘s’. Count of operations = 1 + 10 = 11- Increase 8th character
‘m’by 5 to get‘r’. Count of operations = 11 + 5 = 16.Therefore, the total count of operations is 16.

Input:S = “abccdb”Output:3Explanation:

The possible transformation that given the minimum count of operation is:

- Increase 1st character
‘a’by 1 to get‘b’. Count of operation = 1- Increase 2nd character
‘b’by 2 to get‘d’. Count of operations = 1 + 2 = 3.

**Naive Approach:** The simplest approach is to generate all possible strings of length **N**. Then check for each string, if its a palindrome. If any string is found to be palindromic then find the cost of operation required to convert the given string into that string. Repeat the above steps for all the generated strings and print the minimum cost calculated among all the costs.

**Time Complexity:** O(26^{N})**Auxiliary Space:** O(N)

**Efficient Approach: **To optimize the above approach, the idea is to traverse the given string and find the change for each position independently. Follow the below steps to solve the problem:

- Traverse through the given string over the range
**[0, (N/2) – 1]**. - For each character at index
**i**, find the absolute difference between the characters at indices**i**and**(N – i – 1)**. - There can be two possible differences i.e., the difference when the character at
**i**is incremented to character at index**(N – i – 1)**and when it is decremented at that index. - Take the minimum of both and add it to the result.
- After the above steps, print the minimum cost calculated.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum number ` `// of operations required to convert ` `// th given string into palindrome ` `int` `minOperations(string s) ` `{ ` ` ` `int` `len = s.length(); ` ` ` `int` `result = 0; ` ` ` ` ` `// Iterate till half of the string ` ` ` `for` `(` `int` `i = 0; i < len / 2; i++) { ` ` ` ` ` `// Find the absolute difference ` ` ` `// between the characters ` ` ` `int` `D1 = max(s[i], s[len - 1 - i]) ` ` ` `- min(s[i], s[len - 1 - i]); ` ` ` ` ` `int` `D2 = 26 - D1; ` ` ` ` ` `// Adding the minimum difference ` ` ` `// of the two result ` ` ` `result += min(D1, D2); ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given string ` ` ` `string s = ` `"abccdb"` `; ` ` ` ` ` `// Function Call ` ` ` `cout << minOperations(s); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to find the minimum number ` `// of operations required to convert ` `// th given string into palindrome ` `public` `static` `int` `minOperations(String s) ` `{ ` ` ` `int` `len = s.length(); ` ` ` `int` `result = ` `0` `; ` ` ` ` ` `// Iterate till half of the string ` ` ` `for` `(` `int` `i = ` `0` `; i < len / ` `2` `; i++) ` ` ` `{ ` ` ` ` ` `// Find the absolute difference ` ` ` `// between the characters ` ` ` `int` `D1 = Math.max(s.charAt(i), ` ` ` `s.charAt(len - ` `1` `- i)) - ` ` ` `Math.min(s.charAt(i), ` ` ` `s.charAt(len - ` `1` `- i)); ` ` ` ` ` `int` `D2 = ` `26` `- D1; ` ` ` ` ` `// Adding the minimum difference ` ` ` `// of the two result ` ` ` `result += Math.min(D1, D2); ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given string ` ` ` `String s = ` `"abccdb"` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(minOperations(s)); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find the minimum number ` `# of operations required to convert ` `# th given string into palindrome ` `def` `minOperations(s): ` ` ` ` ` `length ` `=` `len` `(s) ` ` ` `result ` `=` `0` ` ` ` ` `# Iterate till half of the string ` ` ` `for` `i ` `in` `range` `(length ` `/` `/` `2` `): ` ` ` ` ` `# Find the absolute difference ` ` ` `# between the characters ` ` ` `D1 ` `=` `(` `ord` `(` `max` `(s[i], s[length ` `-` `1` `-` `i])) ` `-` ` ` `ord` `(` `min` `(s[i], s[length ` `-` `1` `-` `i]))) ` ` ` ` ` `D2 ` `=` `26` `-` `D1 ` ` ` ` ` `# Adding the minimum difference ` ` ` `# of the two result ` ` ` `result ` `+` `=` `min` `(D1, D2) ` ` ` ` ` `# Return the result ` ` ` `return` `result ` ` ` `# Driver Code ` ` ` `# Given string ` `s ` `=` `"abccdb"` ` ` `# Function call ` `print` `(minOperations(s)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum number ` `// of operations required to convert ` `// th given string into palindrome ` `public` `static` `int` `minOperations(String s) ` `{ ` ` ` `int` `len = s.Length; ` ` ` `int` `result = 0; ` ` ` ` ` `// Iterate till half of the string ` ` ` `for` `(` `int` `i = 0; i < len / 2; i++) ` ` ` `{ ` ` ` ` ` `// Find the absolute difference ` ` ` `// between the characters ` ` ` `int` `D1 = Math.Max(s[i], ` ` ` `s[len - 1 - i]) - ` ` ` `Math.Min(s[i], ` ` ` `s[len - 1 - i]); ` ` ` ` ` `int` `D2 = 26 - D1; ` ` ` ` ` `// Adding the minimum difference ` ` ` `// of the two result ` ` ` `result += Math.Min(D1, D2); ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given string ` ` ` `String s = ` `"abccdb"` `; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(minOperations(s)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Make palindromic string non-palindromic by rearranging its letters
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Minimum characters to be replaced to make a string concatenation of a K-length palindromic string
- Minimize Cost with Replacement with other allowed
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimize characters to be changed to make the left and right rotation of a string same
- Check if frequency of each character is equal to its position in English Alphabet
- Count characters at same position as in English alphabet
- Find the count of palindromic sub-string of a string in its sorted form
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count decrements to nearest smaller element required to make all array elements equal
- Make a palindromic string from given string
- Min flips of continuous characters to make all characters same in a string
- Character replacement after removing duplicates from a string
- Smallest length string with repeated replacement of two distinct adjacent
- Minimize length of prefix of string S containing all characters of another string T
- Longest Palindromic Substring using Palindromic Tree | Set 3

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.