The minimum number of characters that must be replaced to form a given string Palindrome



If there are multiple answers, print the lexicographically smallest line.

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 to (length) / 2-1 and check if the character is at the i-th index, i.e. s [i]! = S [length-i-1], then we will replace the alphabetically larger character with one that is alphabetically smaller among them and continue the same process until all the elements have passed.

Below is the implementation of the above approach:

C ++

// C ++ Implementation of the above approach
# include & lt; bits / stdc ++. h & gt;

using namespace std;

 
// Function for finding the minimum number
// character change required

 

void change (string s)

{

 

// Find the length of the line

int n = s.length (); 

 

// To store the number of replacement operations

  int cc = 0;

 

for ( int i = 0; i & lt; n / 2; i ++)

{

 

// If characters are in place

// then me and ni-1 are the same

// no changes required

if (s [i] == s [ni-1]) 

continue ;

 

// Count one change operation

  cc + = 1;

 

// Change character with higher

  // ascii value with lower ascii value

if (s [i] & lt; s [ni-1]) 

s [ni-1] = s [ i];

else

  s [i] = s [ni-1];

}

printf ( " Minimum characters to be replaced =% d " , (cc));

cout & lt; & lt; s & lt; & lt; endl; 

}
// Driver code

int main ()

{

string s = "geeks" ;

change ((s));

return 0;

}
// provided by Arnab Kundu

Java

// Implementation of the above approach in Java

import java.util. *;

 

class GFG

{

  
// Function for finding the minimum number
// character change required

static void change (String s)

{

 

  // Find line length

int n = s.length (); 

 

// To store the number of replacements

  int cc = 0 ;

 

for ( int i = 0 ; i & lt; n / 2 ; i ++)

{

 

// If characters are in place

// then me and ni-1 are the same

  // no changes required

  if (s.charAt (i) == s.charAt (n - i - 1 )) 

continue ;

 

// Count one change operation

  cc + = 1 ;

 

// Change character with higher

  // ascii value with lower ascii value

if (s.charAt (i ) & lt; 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 provided
// Surendra_Gangwar

python

# Python Implementing the above approach

  
# Minimum number search function
# character change required

import math as ma

def change (s ):

 

# Find line length

n = len (s)

 

# Store the number of replacements

cc = 0

 

for i in range (n / / 2 ):

 

# If characters are in place

  # i and ni-1 are the same

# no changes required

if (s [i] = = s [n - i - 1 ]):

continue

  

  # Counting one change operation

cc + = 1

 

# Change character with taller

# ascii value with lower ascii value

if (s [i] & lt; 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 # Implementing the above approach

using System; 

 

class GFG

{

  
// Function for finding the minimum number
// character change required

static void change (String s)

{

 

  // Find line length

int n = s.Length; 

 

// To save quantity

  // replace operations

int cc = 0;

 

for ( int i = 0; i & lt; n / 2; i ++)

{

 

// If characters are in place

// then me and ni-1 are the same

// no changes required

if (s [i] == s [n - i - 1]) 

continue ;

 

// Count one change operation

  cc + = 1;

 

// Change character with higher

  // ascii value with lower ascii value

if (s [i] & lt ; 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 is provided by Rajput-Ji

PHP

& lt;? php 
// Implementation of the above approach in PHP

 
// Function for finding the minimum numbers
// character change required

 

function change ( $ s )

{

  

// Find im line length

$ n = strlen ( $ s ); 

 

// To store the number of replace operations

  $ cc = 0;

 

for ( $ i = 0; $ i & lt; $ n / 2; $ i ++)

  {

 

  // If characters are in place

  // then me and ni-1 are the same

// no changes required

if ( $ s [ $ i ] == $ s [ $ n - $ i - 1]) 

continue ;

 

// Count one change operation

  $ cc + = 1;

 

// Change character with higher

  // ascii value with lower ascii value

if ( $ s [ $ i ] & lt ; $ s [ $ n - $ i - 1]) 

$ s [ $ n - $ i - 1] = $ s [ $ i ];

else

$ s [ $ i ] = $ s [ $ n - $ i - 1];

}

echo " Minimum characters to be replaced = " . $ cc . "" ;

echo $ s . ""

}
// Driver code

 

$ s = "geeks" ;

change (( $ s )) ;

return 0;

? & gt;

Exit:

 Minimum characters to be replaced = 2 geeeg