Minimum removals required such that a string can be rearranged to form a palindrome
Last Updated :
26 May, 2021
Given a string S consisting of lowercase English alphabets, the task is to find the minimum number of characters required to be removed such that the characters of the string could be rearranged to form a palindrome.
Examples:
Input: S = “ababccca”
Output: 1
Explanation:
Remove the occurrence of ‘c’ from the string. Therefore, the modified string is “ababcca”, which can be rearranged to form the palindromic string “cababac”.
Therefore, only one removal is required.
Input: S = abcde
Output: 4
Approach: The problem can be solved based on the observation that, in a palindromic string, at most one character can occur the odd number of times. Therefore, reduce the frequency of all odd-frequent characters except one of them.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDeletions(string str)
{
int fre[26];
memset (fre, 0, sizeof (fre));
int n = str.size();
cout<<n;
for ( int i = 0; i < n; i++) {
fre[str[i] - 'a' ] += 1;
}
for ( int i = 0; i < n; i++) {
cout<<fre[i];
}
int count = 0;
for ( int i = 0; i < 26; i++) {
if (fre[i] % 2) {
count += 1;
}
}
if (count == 0 || count == 1) {
return 0;
}
else {
return count - 1;
}
}
int main()
{
string str = "ababbccca" ;
cout << minDeletions(str) << endl;
}
|
Java
public class GFG
{
static int minDeletions(String str)
{
int fre[] = new int [ 26 ];
int n = str.length();
for ( int i = 0 ; i < n; i++) {
fre[str.charAt(i) - 'a' ] += 1 ;
}
int count = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
if (fre[i] % 2 == 1 ) {
count += 1 ;
}
}
if (count == 0 || count == 1 ) {
return 0 ;
}
else {
return count - 1 ;
}
}
public static void main (String[] args)
{
String str = "ababbccca" ;
System.out.println(minDeletions(str)) ;
}
}
|
Python3
def minDeletions( str ):
fre = [ 0 ] * 26
n = len ( str )
for i in range (n):
fre[ ord ( str [i]) - ord ( 'a' )] + = 1
count = 0
for i in range ( 26 ):
if (fre[i] % 2 ):
count + = 1
if (count = = 0 or count = = 1 ):
return 0
else :
return count - 1
if __name__ = = '__main__' :
str = "ababbccca"
print (minDeletions( str ))
|
C#
using System;
public class GFG
{
static int minDeletions( string str)
{
int []fre = new int [26];
int n = str.Length;
for ( int i = 0; i < n; i++)
{
fre[str[i] - 'a' ] += 1;
}
int count = 0;
for ( int i = 0; i < 26; i++) {
if (fre[i] % 2 == 1) {
count += 1;
}
}
if (count == 0 || count == 1) {
return 0;
}
else {
return count - 1;
}
}
public static void Main( string [] args)
{
string str = "ababbccca" ;
Console.WriteLine(minDeletions(str)) ;
}
}
|
Javascript
<script>
function minDeletions(str)
{
var fre = new Array(26);
fre.fill(0);
var n = str.length;
for ( var i = 0; i < n; i++) {
fre[str[i].charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
}
var count = 0;
for ( var i = 0; i < 26; i++) {
if (fre[i] % 2) {
count += 1;
}
}
if (count == 0 || count == 1) {
return 0;
}
else {
return count - 1;
}
}
var str = "ababbccca" ;
document.write( minDeletions(str)+ "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...