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++
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_CHAR 26
int minRemoval(string str) {
int hash[MAX_CHAR];
memset (hash, 0, sizeof (hash));
for ( int i = 0; str[i]; i++)
hash[str[i] - 'a' ]++;
int count = 0;
for ( int i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2)
count++;
return (count == 0) ? 0 : count-1;
}
int main() {
string str = "geeksforgeeks" ;
cout << minRemoval(str) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static final int MAX_CHAR = 26 ;
static int minRemoval(String str) {
int hash[] = new int [MAX_CHAR];
Arrays.fill(hash, 0 );
for ( int i = 0 ; i < str.length(); i++)
hash[str.charAt(i) - 'a' ]++;
int count = 0 ;
for ( int i = 0 ; i < MAX_CHAR; i++)
if (hash[i] % 2 == 1 )
count++;
return (count == 0 ) ? 0 : count - 1 ;
}
public static void main(String[] args) {
String str = "geeksforgeeks" ;
System.out.println(minRemoval(str));
}
}
|
Python
def minRemoval(strr):
hash = [ 0 ] * 26
for char in strr:
hash [ ord (char) - ord ( 'a' )] = hash [ ord (char) - ord ( 'a' )] + 1
count = 0
for i in range ( 26 ):
if hash [i] % 2 :
count = count + 1
return 0 if count = = 0 else count - 1
if __name__ = = "__main__" :
strr = "geeksforgeeks" ;
print (minRemoval(strr))
|
C#
using System;
class GFG {
static int MAX_CHAR = 26;
static int minRemoval( string str) {
int []hash = new int [MAX_CHAR];
for ( int i = 0; i < MAX_CHAR; i++)
hash[i] = 0;
for ( int i = 0; i < str.Length; i++)
hash[str[i] - 'a' ]++;
int count = 0;
for ( int i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2 == 1)
count++;
return (count == 0) ? 0 : count - 1;
}
public static void Main() {
string str = "geeksforgeeks" ;
Console.Write(minRemoval(str));
}
}
|
PHP
<?php
function minRemoval( $str )
{
$hash = array_fill (0, 26, 0);
for ( $i = 0; $i < strlen ( $str ); $i ++)
$hash [ord( $str [ $i ]) - 97]++;
$count = 0;
for ( $i = 0; $i < 26; $i ++)
if ( $hash [ $i ] % 2)
$count ++;
return ( $count == 0) ? 0 : $count -1;
}
$str = "geeksforgeeks" ;
echo minRemoval( $str ). "\n" ;
?>
|
Javascript
<script>
var MAX_CHAR = 26
function minRemoval( str)
{
var hash = Array(MAX_CHAR).fill(0);
for ( var i = 0; str[i]; i++)
hash[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
var count = 0;
for ( var i = 0; i < MAX_CHAR; i++)
if (hash[i] % 2)
count++;
return (count == 0) ? 0 : count-1;
}
var str = "geeksforgeeks" ;
document.write( minRemoval(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...