要替换的最小字符数以生成给定的字符串Palindrome
给定字符串str ,任务是找到要替换的最小字符数以生成给定的字符串回文。替换一个字符意味着用相同位置的任何单个字符替换它。我们不允许删除或添加任何字符。
如果有多个答案,请打印按字典顺序最小的字符串。
例子:
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.
方法:从 0 到 (length)/2-1 运行一个循环,并检查第 i 个索引处是否有字符,即 s[i]!=s[length-i-1],然后我们将替换按字母顺序较大的字符字符中按字母顺序较小的字符并继续相同的过程,直到遍历所有元素。
下面是上述方法的实现:
C++
// C++ Implementation of the above approach
#include
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
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-1),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
// Nikhil Gupta
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]
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
Javascript
输出:
Minimum characters to be replaced = 2
geeeg