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.
Input : abcdef Output : 6 Explanation: replace 'a', 'b' and 'c' => cost= 1 + 2 + 3 = 6 Input : aba Output : 0
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.
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.
- Minimum cost to convert given string to consist of only vowels
- Minimum reduce operations to convert a given string into a palindrome
- Minimum cost to convert str1 to str2 with the given operations
- Convert the string into palindrome string by changing only one character.
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Minimum cost to modify a string
- Minimum cost to construct a string
- Minimum cost to traverse from one index to another in the String
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum cost to make a string free of a subsequence
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of given operations required to convert a string to another string
- Minimum number of Appends needed to make a string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum characters to be added at front to make string palindrome
- Convert string X to an anagram of string Y with minimum replacements
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum clicks to convert string X to Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.