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 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 the i-th 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 get traversed.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void change(string s)
{
int n = s.length();
int cc = 0;
for ( int i=0;i<n/2;i++)
{
if (s[i]== s[n-i-1])
continue ;
cc+= 1;
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;
}
int main()
{
string s = "geeks" ;
change((s));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void change(String s)
{
int n = s.length();
int cc = 0 ;
for ( int i = 0 ; i < n/ 2 ; i++)
{
if (s.charAt(i) == s.charAt(n - i - 1 ))
continue ;
cc += 1 ;
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- 1 ),s.charAt(n - i - 1 ));
}
System.out.println( "Minimum characters to be replaced = " +(cc)) ;
System.out.println(s);
}
public static void main(String args[])
{
String s = "geeks" ;
change((s));
}
}
|
Python
import math as ma
def change(s):
n = len (s)
cc = 0
for i in range (n / / 2 ):
if (s[i] = = s[n - i - 1 ]):
continue
cc + = 1
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 = "")
s = "geeks"
change( list (s))
|
C#
using System;
class GFG
{
static void change(String s)
{
int n = s.Length;
int cc = 0;
for ( int i = 0; i < n / 2; i++)
{
if (s[i] == s[n - i - 1])
continue ;
cc += 1;
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);
}
public static void Main(String []args)
{
String s = "geeks" ;
change((s));
}
}
|
PHP
<?php
function change( $s )
{
$n = strlen ( $s );
$cc = 0;
for ( $i =0; $i < $n /2; $i ++)
{
if ( $s [ $i ]== $s [ $n - $i -1])
continue ;
$cc += 1;
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" ;
}
$s = "geeks" ;
change(( $s ));
return 0;
?>
|
Javascript
<script>
function change(s)
{
var n = s.length;
var cc = 0;
for ( var i=0;i<n/2;i++)
{
if (s[i]== s[n-i-1])
continue ;
cc+= 1;
if (s[i]<s[n-i-1])
s[n-i-1]= s[i] ;
else
s[i]= s[n-i-1] ;
}
document.write( "Minimum characters to be replaced = " + (cc)+ "<br>" );
document.write(s.join( '' ) + "<br>" );
}
var s = "geeks" .split( '' );
change((s));
</script>
|
Output:
Minimum characters to be replaced = 2
geeeg
Last Updated :
31 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...