📌  相关文章
📜  通过仅删除单一类型的字符来检查给定的字符串是否可以成为回文

📅  最后修改于: 2022-05-13 01:57:07.551000             🧑  作者: Mango

通过仅删除单一类型的字符来检查给定的字符串是否可以成为回文

给定一个字符串S ,任务是是否可以制作一个字符串 删除相同字符的出现后的回文,任意次数

例子:

方法:该任务可以通过迭代给定字符串中的每个唯一字符来解决,并在不匹配的地方删除它的出现,如果找到有效的回文,在多次删除相同字符的出现后,返回“” 否则返回“”。
请按照以下步骤解决问题:

  • 开始迭代字符串的每个唯一字符,其出现将被删除
  • 使用双指针技术检查不匹配,将l放在字符串的开头,将r放在字符串的结尾
  • 如果S[l] == S[r] ,增加l ,减少r
  • 如果S[l]!= S[r],检查是否 S[l[ == char,执行 l++,否则如果 S[r] == char,执行 r–
  • 如果条件都不成立,则表示给定不能转换为回文

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a palindrome is
// possible or not
string isPossible(string S)
{
    // Stores the length of string
    int n = (int)S.length();
 
    // Stores the unique characters in
    // the string
    set st;
 
    for (int i = 0; i < n; i++) {
        st.insert(S[i]);
    }
 
    // Check if valid palindrome is
    // possible or not
    bool check = false;
 
    // Iterating over unique characters
    // of the string
    for (auto ele : st) {
 
        // Pointers to check the condition
        int low = 0, high = n - 1;
        bool flag = true;
 
        // Iterating over the string
        for (int i = 0; i < n; i++) {
            if (S[low] == S[high]) {
 
                // Updating low and high
                low++;
                high--;
            }
 
            else {
                if (S[low] == ele) {
 
                    // Updating low
                    low++;
                }
                else if (S[high] == ele) {
 
                    // Updating high
                    high--;
                }
                else {
 
                    // It is impossible
                    // to make palindrome
                    // by removing
                    // occurrences of char
                    flag = false;
                    break;
                }
            }
        }
 
        // If palindrome is formed
        // break the loop
        if (flag == true) {
            check = true;
            break;
        }
    }
 
    if (check)
        return "Yes";
    else
        return "No";
}
 
// Driver Code
int main()
{
 
    string S = "abczdzacb";
    cout << isPossible(S);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG
{
   
  // Function to check if a palindrome is
// possible or not
static String isPossible(String S)
{
   
    // Stores the length of string
    int n = S.length();
 
    // Stores the unique characters in
    // the string
    Set st = new HashSet();
     
    for (int i = 0; i < n; i++) {
        st.add(S.charAt(i));
    }
     
    // Check if valid palindrome is
    // possible or not
    boolean check = false;
 
    // Iterating over unique characters
    // of the string
    for (Character ele : st) {
 
        // Pointers to check the condition
        int low = 0, high = n - 1;
        boolean flag = true;
 
        // Iterating over the string
        for (int i = 0; i < n; i++) {
            if (S.charAt(low) == S.charAt(high)) {
 
                // Updating low and high
                low++;
                high--;
            }
 
            else {
                if (S.charAt(low) == ele) {
 
                    // Updating low
                    low++;
                }
                else if (S.charAt(high)== ele) {
 
                    // Updating high
                    high--;
                }
                else {
 
                    // It is impossible
                    // to make palindrome
                    // by removing
                    // occurrences of char
                    flag = false;
                    break;
                }
            }
        }
 
        // If palindrome is formed
        // break the loop
        if (flag == true) {
            check = true;
            break;
        }
    }
 
    if (check)
        return "Yes";
    else
        return "No";
}
 
// Driver Code
    public static void main (String[] args) {
      String S = "abczdzacb";
     
        System.out.println(isPossible(S));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# python program for the above approach
 
# Function to check if a palindrome is
# possible or not
def isPossible(S):
 
    # Stores the length of string
    n = len(S)
 
    # Stores the unique characters in
    # the string
    st = set()
 
    for i in range(0, n):
        st.add(S[i])
 
    # Check if valid palindrome is
    # possible or not
    check = False
 
    # Iterating over unique characters
    # of the string
    for ele in st:
 
        # Pointers to check the condition
        low = 0
        high = n - 1
        flag = True
 
        # Iterating over the string
        for i in range(0, n):
            if (S[low] == S[high]):
 
                # Updating low and high
                low += 1
                high -= 1
 
            else:
                if (S[low] == ele):
 
                    # Updating low
                    low += 1
 
                elif (S[high] == ele):
 
                    # Updating high
                    high -= 1
 
                else:
 
                    # It is impossible
                    # to make palindrome
                    # by removing
                    # occurrences of char
                    flag = False
                    break
 
                # If palindrome is formed
                # break the loop
        if (flag == True):
            check = True
            break
 
    if (check):
        return "Yes"
 
    else:
        return "No"
 
# Driver Code
if __name__ == "__main__":
 
    S = "abczdzacb"
    print(isPossible(S))
 
    # This code is contributed by rakeshsahni


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
   
  // Function to check if a palindrome is
// possible or not
static String isPossible(String S)
{
   
    // Stores the length of string
    int n = S.Length;
 
    // Stores the unique characters in
    // the string
    HashSet st = new HashSet();
     
    for (int i = 0; i < n; i++) {
        st.Add(S[i]);
    }
     
    // Check if valid palindrome is
    // possible or not
    bool check = false;
 
    // Iterating over unique characters
    // of the string
    foreach (char ele in st) {
 
        // Pointers to check the condition
        int low = 0, high = n - 1;
        bool flag = true;
 
        // Iterating over the string
        for (int i = 0; i < n; i++) {
            if (S[low] == S[high]) {
 
                // Updating low and high
                low++;
                high--;
            }
 
            else {
                if (S[low] == ele) {  
 
                    // Updating low
                    low++;
                }
                else if (S[high]== ele) {
 
                    // Updating high
                    high--;
                }
                else {
 
                    // It is impossible
                    // to make palindrome
                    // by removing
                    // occurrences of char
                    flag = false;
                    break;
                }
            }
        }
 
        // If palindrome is formed
        // break the loop
        if (flag == true) {
            check = true;
            break;
        }
    }
 
    if (check)
        return "Yes";
    else
        return "No";
}
 
// Driver Code
    public static void Main(String[] args) {
      String S = "abczdzacb";
     
        Console.WriteLine(isPossible(S));
    }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出:
Yes

时间复杂度 O(n*26)
辅助空间 在)