Minimum removal of characters required such that permutation of given string is a palindrome
Given string str consisting of lowercase letters, the task is to find the minimum number of characters to be deleted from the given string such that any permutation of the remaining string is a palindrome.
Examples:
Input: str=”aba”
Output: 1
Explanation: Removing ‘b’ generates a palindromic string “aa”.
Input: “abab”
Output: 0
Explanation: Permutations “abba”, “baab” of the given string are already palindrome. Therefore, no character needs to be deleted.
Input: “abab”
Output: 0
Approach: Follow the steps below to solve the problem:
- Check if the given string is already a palindrome or not. If found to be true, print 0.
- Otherwise, calculate the frequency of each character in the string using a Hashmap.
- Count the number of characters with odd frequencies and store it in a variable, say k.
- Now, the total number of characters required to be deleted is k-1. Therefore, print k – 1 as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool IsPalindrome(string& str)
{
string s = str;
reverse(str.begin(), str.end());
if (s == str) {
return true ;
}
return false ;
}
void CountDeletions(string& str, int len)
{
if (IsPalindrome(str)) {
cout << 0 << endl;
return ;
}
map< char , int > mp;
for ( int i = 0; i < len; i++) {
mp[str[i]]++;
}
int k = 0;
for ( auto it : mp) {
if (it.second & 1) {
k++;
}
}
cout << k - 1 << endl;
}
int main()
{
string str = "abca" ;
int len = str.length();
CountDeletions(str, len);
}
|
Java
import java.util.*;
class GFG{
static String str;
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
static boolean IsPalindrome()
{
String s = str;
s = reverse(str);
if (s == str)
{
return true ;
}
return false ;
}
static void CountDeletions( int len)
{
if (IsPalindrome())
{
System.out.print( 0 + "\n" );
return ;
}
HashMap<Character,
Integer> mp =
new HashMap<>();
for ( int i = 0 ; i < len; i++)
{
if (mp.containsKey(str.charAt(i)))
{
mp.put(str.charAt(i),
mp.get(str.charAt(i)) + 1 );
}
else
{
mp.put(str.charAt(i), 1 );
}
}
int k = 0 ;
for (Map.Entry<Character,
Integer> it :
mp.entrySet())
{
if (it.getValue() % 2 == 1 )
{
k++;
}
}
System.out.print(k - 1 + "\n" );
}
public static void main(String[] args)
{
str = "abca" ;
int len = str.length();
CountDeletions(len);
}
}
|
Python3
from collections import defaultdict
def IsPalindrome( str ):
s = str
s = s[:: - 1 ]
if (s = = str ):
return True
return False
def CountDeletions( str , ln):
if (IsPalindrome( str )):
print ( 0 )
return
mp = defaultdict( lambda : 0 )
for i in range (ln):
mp[ str [i]] + = 1
k = 0
for it in mp.keys():
if (mp[it] & 1 ):
k + = 1
print (k - 1 )
if __name__ = = '__main__' :
str = "abca"
ln = len ( str )
CountDeletions( str , ln)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String str;
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" , a);
}
static bool IsPalindrome()
{
String s = str;
s = reverse(str);
if (s == str)
{
return true ;
}
return false ;
}
static void CountDeletions( int len)
{
if (IsPalindrome())
{
Console.Write(0 + "\n" );
return ;
}
Dictionary< char ,
int > mp =
new Dictionary< char ,
int >();
for ( int i = 0; i < len; i++)
{
if (mp.ContainsKey(str[i]))
{
mp[str[i]] = mp[str[i]] + 1;
}
else
{
mp.Add(str[i], 1);
}
}
int k = 0;
foreach (KeyValuePair< char ,
int > it in mp)
{
if (it.Value % 2 == 1)
{
k++;
}
}
Console.Write(k - 1 + "\n" );
}
public static void Main(String[] args)
{
str = "abca" ;
int len = str.Length;
CountDeletions(len);
}
}
|
Javascript
<script>
let str;
function reverse(input)
{
let a = input.split( '' );
let l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
let temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return a.join( "" );
}
function IsPalindrome()
{
let s = str;
s = reverse(str);
if (s == str)
{
return true ;
}
return false ;
}
function CountDeletions(len)
{
if (IsPalindrome())
{
document.write(0 + "</br>" );
return ;
}
let mp = new Map();
for (let i = 0; i < len; i++)
{
if (mp.has(str[i]))
{
mp.set(str[i], mp.get(str[i]) + 1);
}
else
{
mp.set(str[i], 1);
}
}
let k = 0;
mp.forEach((values,it)=>{
if (values % 2 == 1)
{
k++;
}
})
document.write((k - 1) + "</br>" );
}
str = "abca" ;
let len = str.length;
CountDeletions(len);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
14 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...