Given a 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 at the same position. We are not allowed to remove or add any characters.
If there are multiple answers, print the lexicographically smallest string.
Examples:
Input: str = "geeks" Output: 2 geeks can be converted to geeeg to make it palindrome by replacing minimum characters. Input: str = "ameba" Output: 1 We can get "abeba" or "amema" with only 1 change. Among those two, "abeba" is lexicographically smallest.
Approach: Run a loop from 0 up to (length)/2-1 and check if a character at ith index i.e. s[i]!=s[length-i-1] then we will replace the alphabetically larger character with the one which is alphabetically smaller among them and continue the same process until all the elements gets traversed.
Below is the implementation of the above approach:
C++
// C++ Implementation of the above approach #include<bits/stdc++.h> using namespace std; // Function to find the minimum number // character change required void change(string s) { // Finding the length of the string int n = s.length(); // To store the number of replacement operations int cc = 0; for(int i=0;i<n/2;i++) { // If the characters at location // i and n-i-1 are same then // no change is required if(s[i]== s[n-i-1]) continue; // Counting one change operation cc+= 1; // Changing the character with higher // ascii value with lower ascii value if(s[i]<s[n-i-1]) s[n-i-1]= s[i] ; else s[i]= s[n-i-1] ; } printf("Minimum characters to be replaced = %d\n", (cc)) ; cout<<s<<endl; } // Driver code int main() { string s = "geeks"; change((s)); return 0; } //contributed by Arnab Kundu |
Java
// Java Implementation of the above approach import java.util.*; class GFG { // Function to find the minimum number // character change required static void change(String s) { // Finding the length of the string int n = s.length(); // To store the number of replacement operations int cc = 0; for(int i = 0; i < n/2; i++) { // If the characters at location // i and n-i-1 are same then // no change is required if(s.charAt(i) == s.charAt(n - i - 1)) continue; // Counting one change operation cc += 1; // Changing the character with higher // ascii value with lower ascii value if(s.charAt(i) < s.charAt(n - i - 1)) s = s.replace(s.charAt(n - i - 1),s.charAt(i)); else s = s.replace(s.charAt(n),s.charAt(n - i - 1)); } System.out.println("Minimum characters to be replaced = "+(cc)) ; System.out.println(s); } // Driver code public static void main(String args[]) { String s = "geeks"; change((s)); } } // This code is contributed by // Surendra_Gangwar |
Python
# Python Implementation of the above approach # Function to find the minimum number # character change required import math as ma def change(s): # Finding the length of the string n = len(s) # To store the number of replacement operations cc = 0 for i in range(n//2): # If the characters at location # i and n-i-1 are same then # no change is required if(s[i]== s[n-i-1]): continue # Counting one change operation cc+= 1 # Changing the character with higher # ascii value with lower ascii value if(s[i]<s[n-i-1]): s[n-i-1]= s[i] else: s[i]= s[n-i-1] print("Minimum characters to be replaced = ", str(cc)) print(*s, sep ="") # Driver code s = "geeks"change(list(s)) |
C#
// C# Implementation of the above approach using System; class GFG { // Function to find the minimum number // character change required static void change(String s) { // Finding the length of the string int n = s.Length; // To store the number of //replacement operations int cc = 0; for(int i = 0; i < n / 2; i++) { // If the characters at location // i and n-i-1 are same then // no change is required if(s[i] == s[n - i - 1]) continue; // Counting one change operation cc += 1; // Changing the character with higher // ascii value with lower ascii value if(s[i] < s[n - i - 1]) s = s.Replace(s[n - i - 1], s[i]); else s = s.Replace(s[n], s[n - i - 1]); } Console.WriteLine("Minimum characters " + "to be replaced = " + (cc)); Console.WriteLine(s); } // Driver code public static void Main(String []args) { String s = "geeks"; change((s)); } } // This code contributed by Rajput-Ji |
PHP
<?php // PHP Implementation of the above approach // Function to find the minimum number // character change required function change($s) { // Finding the length of the string $n = strlen($s); // To store the number of replacement operations $cc = 0; for($i=0;$i<$n/2;$i++) { // If the characters at location // i and n-i-1 are same then // no change is required if($s[$i]== $s[$n-$i-1]) continue; // Counting one change operation $cc+= 1; // Changing the character with higher // ascii value with lower ascii value if($s[$i]<$s[$n-$i-1]) $s[$n-$i-1]= $s[$i] ; else $s[$i]= $s[$n-$i-1] ; } echo "Minimum characters to be replaced = ". $cc."\n" ; echo $s."\n"; } // Driver code $s = "geeks"; change(($s)); return 0; ?> |
Minimum characters to be replaced = 2 geeeg
Recommended Posts:
- Minimum characters to be added at front to make string palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum number of deletions to make a string palindrome
- Print the arranged positions of characters to make palindrome
- Make lexicographically smallest palindrome by substituting missing characters
- Count minimum swap to make string palindrome
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Check if string remains palindrome after removing given number of characters
- Check if characters of a given string can be rearranged to form a palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Minimum removal to make palindrome permutation
- Minimum changes required to make each path in a matrix palindrome
- Find if string is K-Palindrome or not using all characters exactly once
- Check if the characters in a string form a Palindrome in O(1) extra space
- Find the player who rearranges the characters to get a palindrome string first
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Minimum reduce operations to convert a given string into a palindrome
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
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.

