Minimum reduce operations to convert a given string into a palindrome - GeeksforGeeks

21 May, 2021
Given a String find the minimum number of reduce operations required to convert a given string into a palindrome. In a reduce operation, we can change character to a immediate lower value. For example b can be covered to a.Examples :  
Input  :  abcd  
Output :  4
We need to reduce c once
and d three times.

Input  : ccc
Output : 0
 
The idea is simple. We traverse string from left and compare characters of left half with their corresponding characters in right half. We add difference between to characters to result.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to count minimum reduce// operations to make a palindrome#include <bits/stdc++.h>using namespace std; // Returns count of minimum character// reduce operations to make palindrome.int countReduce(string& str){    int n = str.length();    int res = 0;     // Compare every character of first half    // with the corresponding character of    // second half and add difference to    // result.    for (int i = 0; i < n / 2; i++)        res += abs(str[i] - str[n - i - 1]);     return res;} // Driver codeint main(){    string str = "abcd";    cout << countReduce(str);    return 0;}
// Java program to count minimum reduce// operations to make a palindromeimport java.io.*; class GFG{    // Returns count of minimum character    // reduce operations to make palindrome.    static int countReduce(String str)    {        int n = str.length();        int res = 0;             // Compare every character of first half        // with the corresponding character of        // second half and add difference to        // result.        for (int i = 0; i < n / 2; i++)            res += Math.abs(str.charAt(i)                   - str.charAt(n - i - 1));             return res;    }         // Driver code    public static void main (String[] args)    {        String str = "abcd";        System.out.println( countReduce(str));                 }} // This code is contributed by vt_m.
# python3 program to count minimum reduce# operations to make a palindrome # Returns count of minimum character# reduce operations to make palindrome.def countReduce(str):     n = len(str)    res = 0     # Compare every character of first half    # with the corresponding character of    # second half and add difference to    # result.    for i in range(0, int(n/2)):        res += abs( int(ord(str[i])) -               int(ord(str[n - i - 1])) )         return res # Driver codestr = "abcd"print(countReduce(str)) # This code is contributed by Sam007
// C# program to count minimum reduce// operations to make a palindromeusing System; class GFG {         // Returns count of minimum character    // reduce operations to make palindrome.    static int countReduce(string str)    {        int n = str.Length;        int res = 0;             // Compare every character of first        // half with the corresponding        // character of second half and        // add difference to result.        for (int i = 0; i < n / 2; i++)            res += Math.Abs(str[i]                    - str[n - i - 1]);             return res;    }         // Driver code    public static void Main ()    {        string str = "abcd";        Console.WriteLine( countReduce(str));                 }} // This code is contributed by vt_m.
<?php// PHP program to count minimum// reduce operations to make a// palindrome // Returns count of minimum// character reduce operations// to make palindrome.function countReduce($str){    $n = strlen($str);    $res = 0;     // Compare every character    // of first half with the    // corresponding character    // of second half and add    // difference to result.    for ($i = 0; $i < $n / 2; $i++)        $res += abs(ord($str[$i]) -                    ord($str[($n - $i - 1)]));    return $res;} // Driver code$str = "abcd";echo countReduce($str); // This code is contributed by Sam007?>
<script>    // Javascript program to count minimum reduce    // operations to make a palindrome         // Returns count of minimum character    // reduce operations to make palindrome.    function countReduce(str)    {        let n = str.length;        let res = 0;               // Compare every character of first        // half with the corresponding        // character of second half and        // add difference to result.        for (let i = 0; i < parseInt(n / 2, 10); i++)            res += Math.abs(str[i].charCodeAt() - str[n - i - 1].charCodeAt());               return res;    }              let str = "abcd";      document.write(countReduce(str));     </script>
Output :  
4
 
YouTubeGeeksforGeeks500K subscribersMinimum reduce operations to covert a given string into a palindrome | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:27•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=CmTUywvWzyk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Sahil Srivastava. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
Sam007
nidhi_biet
divyesh072019
palindrome
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 String Coding Problems for Interviews
Vigenère Cipher
How to Append a Character to a String in C
Program to add two binary strings
Convert character array to string in C++
Naive algorithm for Pattern Searching
Hill Cipher
Return maximum occurring character in an input string
Print all subsequences of a string
A Program to check if strings are rotations of each other or not