Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Minimum cost to convert string into palindrome
Next article icon

Minimum reduce operations to convert a given string into a palindrome

Last Updated : 02 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 converted 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. 

Implementation:

C++




// 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 code
int main()
{
    string str = "abcd";
    cout << countReduce(str);
    return 0;
}
 
 

Java




// Java program to count minimum reduce
// operations to make a palindrome
import 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




# 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 code
str = "abcd"
print(countReduce(str))
 
# This code is contributed by Sam007
 
 

C#




// C# program to count minimum reduce
// operations to make a palindrome
using 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
// 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
?>
 
 

Javascript




<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

Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(1)

 



Next Article
Minimum cost to convert string into palindrome
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • DSA
  • Strings
  • palindrome
Practice Tags :
  • palindrome
  • Strings

Similar Reads

  • Minimum cost to convert the given String into Palindrome
    Given a string S of length N and 2 integers X and Y, the task is to find the minimum cost of converting the string to a palindrome by performing the following operations any number of times in any order: Move the leftmost character to the rightmost end of the string at cost X. i.e S1S2...SN gets con
    15+ min read
  • 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 p
    4 min read
  • Minimize cost to convert given string to a palindrome
    Given a string S of length N and an integer P denoting a pointer to Pth index of the string, the task is to find the minimum cost to convert the string into a palindrome by performing the following operations: Pointer P can be moved from index i to index j and the cost required is |i - j| where 0 ?
    9 min read
  • Minimum number of characters to be replaced to make a given string Palindrome
    Given string str, the task is to find the minimum number of characters to be replaced to make a given string palindrome. Replacing a character means replacing it with any single character in the same position. We are not allowed to remove or add any characters. If there are multiple answers, print t
    5 min read
  • Minimum cuts required to convert a palindromic string to a different palindromic string
    Given palindromic string s, the task is to find minimum k, such that you can cut this string into k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. If it is impossible then print -1.Examples: Input : string = "civic
    15+ min read
  • Minimize operations to make String palindrome by incrementing prefix by 1
    Given a string S of numbers of length N, the task is to find the minimum number of operations required to change a string into palindrome and we can perform the following procedure on the string : Choose an index i (0 ? i < N) and for all 0 ? j ? i, set Sj = Sj + 1 (i.e. add 1 to every element in
    5 min read
  • Minimizing Operations to Prevent Palindrome Formation
    Given a string S of length N (N >= 2). You can choose any index and replace the character at that index with any other character of your choice any number of times, then the task is to minimize the number of operations (including zero), after that, all the characters can't make an S palindrome wh
    9 min read
  • Minimum number of deletions to make a string palindrome | Set 2
    Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome. Examples: Input : baca Output : 1 Input : geek Output : 2 We have discussed one approach in below post. Minimum number of deletions to make a string palindrome Below approach will use
    9 min read
  • Minimum Count of Bit flips required to make a Binary String Palindromic
    Given an integer N, the task is to find the minimum number of bits required to be flipped to convert the binary representation of N into a palindrome. Examples: Input: N = 12 Output: 2 Explanation: Binary String representing 12 = "1100". To make "1100" a palindrome, convert the string to "0110". The
    7 min read
  • Generate number with given operation and check if it is palindrome
    Given an integer N the task is to create a string out of it by repeating the number such that the length of the resultant string is equal to the sum of the digits in the original number. For eg: If the number is 61 and the sum of the digits is 6 + 1 = 7 so the string generated after repeating 61 wil
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences