📌  相关文章
📜  检查是否可以通过删除最多 K 个字符来使给定字符串的排列成为回文

📅  最后修改于: 2021-10-28 01:31:53             🧑  作者: Mango

给定一个字符串str和一个整数K ,任务是通过从给定字符串删除至多K 个字符来检查给定字符串的排列是否可以成为回文。

例子:

方法:可以使用Hashing解决问题。我们的想法是迭代给定的字符串的字符和存储给定的字符串的每个不同的字符的频率。如果具有奇数频率的给定字符串的不同字符数小于或等于(K + 1) ,则打印Yes 。否则,打印No 。请按照以下步骤解决问题:

  • 初始化一个数组,比如cntFreq[]来存储str的每个字符的频率。
  • 遍历给定的字符串并将字符串str的每个不同字符的频率存储在cntFreq[]数组中。
  • 初始化一个变量,比如cntOddFreq来存储给定字符串的不同字符的计数,其频率为奇数。
  • 遍历 cntFreq[] 数组并检查cntFreq[i] % 2 == 1然后将cntOddFreq的值增加1
  • 最后,检查是否cntOddFreq ≤ (K + 1)然后打印True
  • 否则,打印False

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if
// the string satisfies
// the given conditions or not
bool checkPalinK(string str, int K)
{
    // Stores length of
    // given string
    int N = str.length();
 
    // Stores frequency of
    // each character of str
    int cntFreq[256] = { 0 };
 
    for (int i = 0; i < N;
         i++) {
 
        // Update frequency of
        // current character
        cntFreq[str[i]]++;
    }
 
    // Stores count of
    // distinct character
    // whose frequency is odd
    int cntOddFreq = 0;
 
    // Traverse the cntFreq[]
    // array.
    for (int i = 0; i < 256;
         i++) {
 
        // If frequency of
        // character i is odd
        if (cntFreq[i] % 2
            == 1) {
 
            // Update cntOddFreq
            cntOddFreq++;
        }
    }
 
    // If count of distinct character
    // having odd frequency is <= K + 1
    if (cntOddFreq <= (K + 1)) {
        return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    string str = "geeksforgeeks";
    int K = 2;
 
    // If str satisfy
    // the given conditions
    if (checkPalinK(str, K)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if
// the string satisfies
// the given conditions or not
public static boolean checkPalinK(String str,
                                  int K)
{
     
    // Stores length of
    // given string
    int N = str.length();
 
    // Stores frequency of
    // each character of str
    int cntFreq[] = new int[256];
 
    for(int i = 0; i < N; i++)
    {
         
        // Update frequency of
        // current character
        cntFreq[str.charAt(i)]++;
    }
 
    // Stores count of
    // distinct character
    // whose frequency is odd
    int cntOddFreq = 0;
 
    // Traverse the cntFreq[]
    // array.
    for(int i = 0; i < 256; i++)
    {
         
        // If frequency of
        // character i is odd
        if (cntFreq[i] % 2 == 1)
        {
             
            // Update cntOddFreq
            cntOddFreq++;
        }
    }
 
    // If count of distinct character
    // having odd frequency is <= K + 1
    if (cntOddFreq <= (K + 1))
    {
        return true;
    }
    return false;
}
 
// Driver Code
public static void main(String args[])
{
    String str = "geeksforgeeks";
    int K = 2;
 
    // If str satisfy
    // the given conditions
    if (checkPalinK(str, K))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by hemanth gadarla


Python3
# Python3 program to implement
# the above approach
 
# Function to check if the
# satisfies the given
# conditions or not
def checkPalinK(str, K):
     
    # Stores length of
    # given string
    N = len(str)
 
    # Stores frequency of
    # each character of str
    cntFreq = [0] * 256
 
    for i in range(N):
         
        # Update frequency of
        # current character
        cntFreq[ord(str[i])] += 1
 
    # Stores count of
    # distinct character
    # whose frequency is odd
    cntOddFreq = 0
 
    # Traverse the cntFreq[]
    # array.
    for i in range(256):
 
        # If frequency of
        # character i is odd
        if (cntFreq[i] % 2 == 1):
 
            # Update cntOddFreq
            cntOddFreq += 1
 
    # If count of distinct character
    # having odd frequency is <= K + 1
    if (cntOddFreq <= (K + 1)):
        return True
 
    return False
 
# Driver Code
if __name__ == '__main__':
     
    str = "geeksforgeeks"
    K = 2
 
    # If str satisfy
    # the given conditions
    if (checkPalinK(str, K)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check if
// the string satisfies
// the given conditions or not
public static bool checkPalinK(String str,
                               int K)
{
     
    // Stores length of
    // given string
    int N = str.Length;
 
    // Stores frequency of
    // each character of str
    int []cntFreq = new int[256];
 
    for(int i = 0; i < N; i++)
    {
         
        // Update frequency of
        // current character
        cntFreq[str[i]]++;
    }
 
    // Stores count of
    // distinct character
    // whose frequency is odd
    int cntOddFreq = 0;
 
    // Traverse the cntFreq[]
    // array.
    for(int i = 0; i < 256; i++)
    {
         
        // If frequency of
        // character i is odd
        if (cntFreq[i] % 2 == 1)
        {
             
            // Update cntOddFreq
            cntOddFreq++;
        }
    }
 
    // If count of distinct character
    // having odd frequency is <= K + 1
    if (cntOddFreq <= (K + 1))
    {
        return true;
    }
    return false;
}
 
// Driver Code
public static void Main(String []args)
{
    String str = "geeksforgeeks";
    int K = 2;
 
    // If str satisfy
    // the given conditions
    if (checkPalinK(str, K))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
Yes

时间复杂度: O(N + 256),其中 N 是给定字符串的长度。
辅助空间: O(256)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程