# Minimum cost to convert string into palindrome

Convert string S into a palindrome string. You can only replace a character with any other character. When you replace character ‘a’ with any other character, it costs 1 unit, similarly for ‘b’ it is 2 units ….. and for ‘z’, it is 26 units. Find the minimum cost required to convert string S into palindrome string.

Examples :

```Input : abcdef
Output : 6
Explanation: replace 'a', 'b' and
'c' => cost= 1 + 2 + 3 = 6

Input : aba
Output : 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to start comparing from the two ends of string. Let i be initialized as 0 index and j initialized as length – 1. If characters at two indices are not same, a cost will apply. To make the cost minimum replace the character which is smaller. Then increment i by 1 and decrement j by 1. Iterate till i less than j.

## C++

 `// CPP program to find minimum cost to make ` `// a palindrome. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return cost ` `int` `cost(string str) ` `{ ` `    ``// length of string ` `    ``int` `len = str.length();       ` `     `  `    ``// Iterate from  both sides of string. ` `    ``// If not equal, a cost will be there ` `    ``int` `res = 0; ` `    ``for` `(``int` `i=0, j=len-1; i < j; i++, j--)          ` `        ``if` `(str[i] != str[j])  ` `            ``res += min(str[i], str[j]) - ``'a'` `+ 1;         ` `     `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"abcdef"``; ` `    ``cout << cost(str) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find minimum cost to make ` `// a palindrome. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `    ``// Function to return cost ` `    ``static` `int` `cost(String str) ` `    ``{ ` `        ``// length of string ` `        ``int` `len = str.length();  ` `         `  `        ``// Iterate from both sides of string. ` `        ``// If not equal, a cost will be there ` `        ``int` `res = ``0``; ` `        ``for` `(``int` `i = ``0``, j = len - ``1``; i < j; i++, j--)      ` `            ``if` `(str.charAt(i) != str.charAt(j))  ` `                ``res += Math.min(str.charAt(i), str.charAt(j))  ` `                                ``- ``'a'` `+ ``1``;      ` `         `  `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``String str = ``"abcdef"``; ` `        ``System.out.println(cost(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# python program to find minimum ` `# cost to make a palindrome. ` ` `  `# Function to return cost ` `def` `cost(st): ` `     `  `    ``# length of string ` `    ``l ``=` `len``(st)      ` `     `  `    ``# Iterate from both sides ` `    ``# of string. If not equal, ` `    ``# a cost will be there ` `    ``res ``=` `0` `    ``j ``=` `l ``-` `1` `    ``i ``=` `0` `    ``while``(i < j):          ` `        ``if` `(st[i] !``=` `st[j]): ` `            ``res ``+``=` `(``min``(``ord``(st[i]),  ` `                    ``ord``(st[j])) ``-`  `                     ``ord``(``'a'``) ``+` `1``) ` `             `  `        ``i ``=` `i ``+` `1` `        ``j ``=` `j ``-` `1` `         `  `    ``return` `res ` ` `  `# Driver code ` `st ``=` `"abcdef"``; ` `print``(cost(st)) ` ` `  `# This code is contributed by ` `# Sam007 `

## C#

 `// C# program to find minimum cost ` `// to make a palindrome. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return cost ` `    ``static` `int` `cost(String str) ` `    ``{ ` `        ``// length of string ` `        ``int` `len = str.Length;  ` `         `  `        ``// Iterate from both sides of string. ` `        ``// If not equal, a cost will be there ` `        ``int` `res = 0; ` `        ``for` `(``int` `i = 0, j = len - 1; i < j; i++, j--)      ` `            ``if` `(str[i] != str[j])  ` `                ``res += Math.Min(str[i], str[j])  ` `                                ``- ``'a'` `+ 1;  ` `         `  `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``string` `str = ``"abcdef"``; ` `        ``Console.WriteLine(cost(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```6
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : vt_m, Sam007

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.