Skip to content
Related Articles

Related Articles

Improve Article
Check if permutation of a given string can be made palindromic by removing at most K characters
  • Last Updated : 19 Apr, 2021

Given a string str and an integer K, the task is to check if a permutation of the given string can be made a palindromic by removing at most K characters from the given string.

Examples:

Input: str = “geeksforgeeks”, K = 2 
Output: Yes 
Explanation: 
Removing (str[5], str[6]) from the given string makes the remaining string “geeksrgeeks” palindromic. Therefore, the required output is Yes. 

Input: str = “coder”, K = 1 
Output: No 
 

 

Approach: The problem can be solved using Hashing. The idea is to iterate over characters of the given string and store the frequency of each distinct character of the given string. If the count of distinct characters of the given string having odd frequency is less than or equal to (K + 1), then print Yes. Otherwise, print No. Follow the steps below to solve the problem:



Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
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




<script>
 
// Javascript program to implement
// the above approach
 
// Function to check if
// the string satisfies
// the given conditions or not
function checkPalinK(str, K)
{
    // Stores length of
    // given string
    var N = str.length;
 
    // Stores frequency of
    // each character of str
    var cntFreq =  Array(256).fill(0);
 
    var i;
    for (i = 0; i < N;
         i++) {
 
        // Update frequency of
        // current character
        cntFreq[str[i]] += 1;
    }
 
    // Stores count of
    // distinct character
    // whose frequency is odd
    var cntOddFreq = 0;
 
    // Traverse the cntFreq[]
    // array.
    for (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
 
    var str = "geeksforgeeks";
    var K = 2;
 
    // If str satisfy
    // the given conditions
    if (checkPalinK(str, K)) {
        document.write("Yes");
    }
    else {
        document.write("No");
    }
 
</script>
Output: 
Yes

 

Time Complexity: O(N + 256), Where N is the length of the given string.
Auxiliary Space: O(256)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :