Given a string S, we have to find minimum characters that we can remove to make any permutation of the string S a palindrome.
In simple terms, the problem states that: Make the string a palindrome by rearranging it in any way by removing the minimum number of characters including removing 0 number of character if possible.
Note : we are considering only small alphabets.
Input : geeksforgeeks Output : 2 Explanation : if we remove 2 characters lets say 'f' and 'r', we remain with "geeksogeeks" which can be re-arranged like "skeegogeeks" to make it a palindrome. Removal of less than 2 character wouldn't make this string a palindrome. Input : shubham Output : 4 If we remove any 4 characters except 'h' (let's say 's', 'b', 'a', 'm'), we remain with "huh" which is a palindrome.
A Naive approach would check every permutation of the string for palindrome and if not found then remove one character and check again. This approach is very complicated and will take a lot of time.
A efficient approach would be to notice that we don’t need to print the minimum characters, just the minimum number. So, an effective idea is the key that: there can be two types of a palindrome, even length, and odd length palindrome. We can deduce the fact that an even length palindrome must have every character occurring even number of times(i.e. the frequency of every character is even). Similarly, an odd palindrome must have every character occurring even number of times except one character occurring odd number of times.
From these facts, the problem turn out to be quite simple. We check frequency of every character and those characters occurring odd number of times are then counted. Then the result is total count of odd frequency characters subtraction 1.
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.
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum changes required to make each path in a matrix palindrome
- Count minimum swap to make string palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum characters to be added at front to make string palindrome
- Maximum even length sub-string that is permutation of a palindrome
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Make a lexicographically smallest palindrome with minimal changes
- Make largest palindrome by changing at most K-digits
- Remove a character from a string to make it a palindrome
- Print the arranged positions of characters to make palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Make lexicographically smallest palindrome by substituting missing characters
- Minimum insertions to form a palindrome | DP-28
- Minimum insertions to form shortest palindrome
- Minimum cost to convert string into palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.