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 = “neveropen”, K = 2
Output: Yes
Explanation:
Removing (str[5], str[6]) from the given string makes the remaining string “neveropenrneveropen” 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:
- Initialize an array, say cntFreq[] to store the frequency of each character of str.
- Traverse the given string and store the frequency of each distinct character of the string str in cntFreq[] array.
- Initialize a variable, say cntOddFreq to store the count of distinct characters of the given string whose frequency is an odd number.
- Traverse the cntFreq[] array and check if cntFreq[i] % 2 == 1 then increment the value of cntOddFreq by 1.
- Finally, check if cntOddFreq ≤ (K + 1) then print True.
- Otherwise, print False.
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 = "neveropen" ; 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 = "neveropen" ; 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 string # 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 = "neveropen" 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 = "neveropen" ; 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 = "neveropen" ; var K = 2; // If str satisfy // the given conditions if (checkPalinK(str, K)) { document.write( "Yes" ); } else { document.write( "No" ); } </script> |
Yes
Time Complexity: O(N + 256), Where N is the length of the given string.
Auxiliary Space: O(256)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!