检查给定的字符串是否可以通过仅删除单一类型的字符来制作回文 |第 2 组
给定一个字符串S ,任务是是否可以制作一个字符串 删除相同字符的出现后的回文,任意次数
例子:
Input: S = “abczdzacb“
Output: Yes
Explanation: Remove first and second occurrence of character ‘a’.
String S becomes “bczdzcb”, which is a palindrome.
Input: S = “madem”
Output: No
Explanation: Since only we can remove 1 character of any frequency only once.
There is no such character removing which a palindrome can be made.
方法:这个问题可以利用回文的性质来解决。很明显,从整个中删除相同字符的出现不会影响回文性质。因此,也可以删除该字符的全部出现。因此,如果它们不相同,请从两端检查字符串,然后在删除两端的整个字符出现后检查字符串。如果任何删除满足回文条件,那么答案是Yes else No 。
请按照以下步骤解决问题:
- 使用(0, N/2)范围内的循环进行迭代
- 现在检查两端的字符是否相等
- 如果字符不相同
- 从两端删除整个出现的字符后检查它是否是回文。
- 如果从任何一端删除所有出现的字符是回文,则打印“YES”并停止迭代。
- 如果字符不相同
- 如果在整个遍历之后,没有找到这样的回文,则打印“NO” 。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to check whether the string
// is palindrome after removal or
// neglecting character c
bool check_palindrome(string str, char c)
{
int n = str.length(), i = 0, j = n - 1;
while (i < j) {
// If it is same as c neglect
// this and move front
if (str[i] == c)
i++;
// If it is same as c neglect
// this and move back
else if (str[j] == c)
j--;
// If they are not same it is
// not a palindrome so return 0
else if (str[i] != str[j])
return 0;
// It can be a palindrome so move
// and check for remaining string
else
i++, j--;
}
return 1;
}
// Function to check if it is possible to
// form a palindrome after removal of
// any number of same characters once
string make_palindrome(string str)
{
bool is_palindrome = 1;
int n = str.length();
// If n==1 || n==2 it is always possible
if (n == 1 || n == 2) {
return "YES";
}
// Check the character from both the ends
// of the string
for (int i = 0; i < n / 2; ++i) {
// If the characters are not equal
if (str[i] != str[n - 1 - i]) {
// Remove str[i] and check if
// it is a palindrome or
// Remove str[n-i-1] and check
// if it is a palindrome
is_palindrome
= check_palindrome(str, str[i])
|| check_palindrome(
str, str[n - 1 - i]);
break;
}
}
if (is_palindrome)
return "Yes";
else
return "No";
}
// Driver Code
int main()
{
string S = "madem";
string res = make_palindrome(S);
cout << (res);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to check whether the string
// is palindrome after removal or
// neglecting character c
static boolean check_palindrome(String str, char c)
{
int n = str.length(), i = 0, j = n - 1;
while (i < j)
{
// If it is same as c neglect
// this and move front
if (str.charAt(i) == c)
i++;
// If it is same as c neglect
// this and move back
else if (str.charAt(j) == c)
j--;
// If they are not same it is
// not a palindrome so return 0
else if (str.charAt(i) != str.charAt(j))
return false;
// It can be a palindrome so move
// and check for remaining string
else {
i++;
j--;
}
}
return true;
}
// Function to check if it is possible to
// form a palindrome after removal of
// any number of same characters once
static String make_palindrome(String str)
{
boolean is_palindrome = true;
int n = str.length();
// If n==1 || n==2 it is always possible
if (n == 1 || n == 2) {
return "YES";
}
// Check the character from both the ends
// of the string
for (int i = 0; i < n / 2; ++i) {
// If the characters are not equal
if (str.charAt(i) != str.charAt(n - 1 - i)) {
// Remove str[i] and check if
// it is a palindrome or
// Remove str[n-i-1] and check
// if it is a palindrome
is_palindrome
= check_palindrome(str, str.charAt(i))
|| check_palindrome(
str, str.charAt(n - 1 - i));
break;
}
}
if (is_palindrome)
return "Yes";
else
return "No";
}
// Driver Code
public static void main(String args[])
{
String S = "madem";
String res = make_palindrome(S);
System.out.println(res);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to check whether the string
# is palindrome after removal or
# neglecting character c
def check_palindrome (str, c):
n = len(str)
i = 0
j = n - 1
while (i < j):
# If it is same as c neglect
# this and move front
if (str[i] == c):
i += 1
# If it is same as c neglect
# this and move back
elif (str[j] == c):
j -= 1
# If they are not same it is
# not a palindrome so return 0
elif (str[i] != str[j]):
return 0
# It can be a palindrome so move
# and check for remaining string
else:
i += 1
j -= 1
return 1
# Function to check if it is possible to
# form a palindrome after removal of
# any number of same characters once
def make_palindrome (str):
is_palindrome = 1
n = len(str)
# If n==1 || n==2 it is always possible
if (n == 1 or n == 2):
return "YES"
# Check the character from both the ends
# of the string
for i in range(n // 2):
# If the characters are not equal
if (str[i] != str[n - 1 - i]):
# Remove str[i] and check if
# it is a palindrome or
# Remove str[n-i-1] and check
# if it is a palindrome
is_palindrome = check_palindrome(str, str[i]) or check_palindrome(str, str[n - 1 - i])
break
if (is_palindrome):
return "Yes"
else:
return "No"
# Driver Code
S = "madem"
res = make_palindrome(S)
print(res)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check whether the string
// is palindrome after removal or
// neglecting character c
static bool check_palindrome(string str, char c)
{
int n = str.Length, i = 0, j = n - 1;
while (i < j)
{
// If it is same as c neglect
// this and move front
if (str[i] == c)
i++;
// If it is same as c neglect
// this and move back
else if (str[j] == c)
j--;
// If they are not same it is
// not a palindrome so return 0
else if (str[i] != str[j])
return false;
// It can be a palindrome so move
// and check for remaining string
else {
i++;
j--;
}
}
return true;
}
// Function to check if it is possible to
// form a palindrome after removal of
// any number of same characters once
static string make_palindrome(string str)
{
bool is_palindrome = true;
int n = str.Length;
// If n==1 || n==2 it is always possible
if (n == 1 || n == 2) {
return "YES";
}
// Check the character from both the ends
// of the string
for (int i = 0; i < n / 2; ++i) {
// If the characters are not equal
if (str[i] != str[n - 1 - i]) {
// Remove str[i] and check if
// it is a palindrome or
// Remove str[n-i-1] and check
// if it is a palindrome
is_palindrome
= check_palindrome(str, str[i])
|| check_palindrome(
str, str[n - 1 - i]);
break;
}
}
if (is_palindrome)
return "Yes";
else
return "No";
}
// Driver Code
public static void Main()
{
string S = "madem";
string res = make_palindrome(S);
Console.Write(res);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
No
时间复杂度: O(N),其中 N 是字符串的长度。
空间复杂度: O(1)