Minimum removal to make palindrome permutation
Last Updated :
22 Nov, 2022
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.
Examples :
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 a palindrome and if not found then remove one character and check again. This approach is very complicated and will take a lot of time.
An Efficient approach: Notice that we don't need to print the minimum characters, just the minimum number. So, an effective idea is a key that: there can be two types of 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.

Implementation:
C++
// CPP Program to find minimum number of removal to
// make any permutation of the string a palindrome
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_CHAR 26
// function to find minimum removal of characters
int minRemoval(string str) {
// hash to store frequency of each character
int hash[MAX_CHAR];
// to set hash array to zeros
memset(hash, 0, sizeof(hash));
// count frequency of each character
for (int i = 0; str[i]; i++)
hash[str[i] - 'a']++;
// count the odd frequency characters
int count = 0;
for (int i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2)
count++;
// if count is -1 return 0
// otherwise return count
return (count == 0) ? 0 : count-1;
}
// Driver's Code
int main() {
string str = "geeksforgeeks";
cout << minRemoval(str) << endl;
return 0;
}
Java
// Java Program to find minimum number of removal to
// make any permutation of the string a palindrome
import java.util.Arrays;
class GFG {
static final int MAX_CHAR = 26;
// function to find minimum removal of characters
static int minRemoval(String str) {
// hash to store frequency of each character
int hash[] = new int[MAX_CHAR];
// to set hash array to zeros
Arrays.fill(hash, 0);
// count frequency of each character
for (int i = 0; i < str.length(); i++)
hash[str.charAt(i) - 'a']++;
// count the odd frequency characters
int count = 0;
for (int i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2 == 1)
count++;
// if count is -1 return 0
// otherwise return count
return (count == 0) ? 0 : count - 1;
}
// Driver code
public static void main(String[] args) {
String str = "geeksforgeeks";
System.out.println(minRemoval(str));
}
}
// This code is contributed by Anant Agarwal.
Python
# Python Program to find minimum number of
# removal to make any permutation of the
# string a palindrome
# function to find minimum removal of
# characters
def minRemoval(strr):
# hash to store frequency of each character
# to set hash array to zeros
hash = [0] * 26
# count frequency of each character
for char in strr:
hash[ord(char)-ord('a')] = hash[ord(char)-ord('a')] + 1
# count the odd frequency characters
count = 0
for i in range(26):
if hash[i]% 2:
count = count + 1
# if count is 0, return 0
# otherwise return count
return 0 if count == 0 else count-1
# Driver's Code
if __name__ == "__main__":
strr = "geeksforgeeks";
# minRemoval to find minimum characters to remove
print(minRemoval(strr))
C#
// C# Program to find minimum number of
// removal to make any permutation of
// the string a palindrome
using System;
class GFG {
static int MAX_CHAR = 26;
// function to find minimum removal
// of characters
static int minRemoval(string str) {
// hash to store frequency of
// each character
int []hash = new int[MAX_CHAR];
// to set hash array to zeros
for(int i = 0; i < MAX_CHAR; i++)
hash[i] = 0;
// count frequency of each character
for (int i = 0; i < str.Length; i++)
hash[str[i] - 'a']++;
// count the odd frequency characters
int count = 0;
for (int i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2 == 1)
count++;
// if count is -1 return 0
// otherwise return count
return (count == 0) ? 0 : count - 1;
}
// Driver code
public static void Main() {
string str = "geeksforgeeks";
Console.Write(minRemoval(str));
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP Program to find minimum
// number of removal to make any
// permutation of the string a palindrome
// function to find minimum
// removal of characters
function minRemoval($str)
{
// hash to store frequency of each
// character and to set hash array to zeros
$hash = array_fill(0, 26, 0);
// count frequency of each character
for ($i = 0; $i < strlen($str); $i++)
$hash[ord($str[$i]) - 97]++;
// count the odd frequency characters
$count = 0;
for ($i = 0; $i < 26; $i++)
if ($hash[$i] % 2)
$count++;
// if count is -1 return 0
// otherwise return count
return ($count == 0) ? 0 : $count-1;
}
// Driver Code
$str = "geeksforgeeks";
echo minRemoval($str)."\n";
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript Program to find minimum number of removal to
// make any permutation of the string a palindrome
var MAX_CHAR = 26
// function to find minimum removal of characters
function minRemoval( str)
{
// hash to store frequency of each character
var hash = Array(MAX_CHAR).fill(0);
// count frequency of each character
for (var i = 0; str[i]; i++)
hash[str[i].charCodeAt(0) - 'a'.charCodeAt(0)]++;
// count the odd frequency characters
var count = 0;
for (var i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2)
count++;
// if count is -1 return 0
// otherwise return count
return (count == 0) ? 0 : count-1;
}
// Driver's Code
var str = "geeksforgeeks";
document.write( minRemoval(str));
// This code is contributed by itsok.
</script>
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
Palindrome String Coding Problems A string is called a palindrome if the reverse of the string is the same as the original one.Example: âmadamâ, âracecarâ, â12321â.Palindrome StringProperties of a Palindrome String:A palindrome string has some properties which are mentioned below:A palindrome string has a symmetric structure which m
2 min read
Palindrome String Given a string s, the task is to check if it is palindrome or not.Example:Input: s = "abba"Output: 1Explanation: s is a palindromeInput: s = "abc" Output: 0Explanation: s is not a palindromeUsing Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left) and
13 min read
Check Palindrome by Different Language
Easy Problems on Palindrome
Sentence Palindrome Given a sentence s, the task is to check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters, such as a word, phrase, or series of symbols, that reads the same backward as forward after converting all uppercase letters to lowercase and removing all non-alphanumer
9 min read
Check if actual binary representation of a number is palindrome Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0âs are being considered. Examples : Input : 9 Output : Yes (9)10 = (1001)2 Inp
6 min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co
5 min read
Check if characters of a given string can be rearranged to form a palindrome Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P
14 min read
Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact
13 min read