# Minimum reduce operations to covert a given string into a palindrome

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++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

This article is contributed by **Sahil Srivastava**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Covert string X to an anagram of string Y with minimum replacements
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of given operations required to convert a string to another string
- Minimum cost to convert string into palindrome
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum characters to be added at front to make string palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Reduce the string by removing K consecutive identical characters
- Convert the string into palindrome string by changing only one character.