Input: str = “geeksforgeeks”, K = 2
Removing (str, str) from the given string makes the remaining string “geeksrgeeks” palindromic. Therefore, the required output is Yes.
Input: str = “coder”, K = 1
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:
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.
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Check whether the number can be made palindromic after adding K
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if all strings of an array can be made same by interchanging characters
- Check if a given string is made up of two alternating characters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Check if a two character string can be made using given words
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Check if string remains palindrome after removing given number of characters
- Check if a string is made up of K alternating characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Permutation of given string that maximizes count of Palindromic substrings
- Check if string can be made lexicographically smaller by reversing any substring
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Make palindromic string non-palindromic by rearranging its letters
- Find the character made by adding all the characters of the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.