Minimum removals required such that a string can be rearranged to form a palindrome


In this problem, we need to remove the minimum characters from the string and rearrange the remaining characters to make the string palindromic.

To solve the problem, the first question we should ask ourselves is when we can make string palindromic. In below two cases, we can make string palindromic.

  • If each character’s frequency is even in the given string.

  • If only one character’s frequency is odd and all other characters' frequency is even.

So, we need to remove minimum characters to make each character’s frequency even except for any single character.

Problem statement − We have given a string str containing N lowercase alphabetic characters. We need to remove minimum characters from the given string so that we can make a palindrome string by rearranging the remaining characters.

Sample Examples

Input –  str = "abcabccd"
Output – 1

Explanation

Here, we need to remove the ‘c’ or ‘d’ character.

Input –  str = ‘aabbc’
Output – 0

Explanation

We don’t need to remove any character as we can make an ‘abcba’ palindrome string from the given string.

Input –  str = ‘pqrstvu’
Output – 6

Explanation

To make a given string a palindrome, we must remove all characters except one. We can make a palindromic string of length 1 only using this string.

Approach 1

In this approach, we will count the frequency of each character in the given string. After that, we will count the total number of characters with odd frequency. We need to keep only 1 character with odd frequency and remove other characters to make their frequency even.

Algorithm

  • Step 1 − Define the ‘freq’ array of length equal to 26.

  • Step 2 − Use the memset() method to initialize all array elements with 0.

  • Step 3 − Iterate through the string and store the frequency of each character in the ‘freq’ array.

  • Step 4 − Initialize the ‘cnt’ variable with 0.

  • Step 5 − Now, iterate through the ‘freq’ array, and increase the value of ‘cnt’ by 1 if the value at the current index is odd in the array.

  • Step 6 − if the final value of the ‘cnt’ variable is 0 or 1, return 0.

  • Step 7 − return ‘cnt – 1’, as we can keep a single character with odd frequency.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the minimum number of deletions required to make string palindrome
int deletionCount(string str){    
   int fre[26]; // array of size 26, which stores the frequency of each character
   memset(fre, 0, sizeof(fre));     // Initialize array with 0
   int n = str.size();
   
   // cnt frequency of each character in the string
   for (int i = 0; i < n; i++){
      fre[str[i] - 'a'] += 1;
   }
   int cnt = 0;
   
   // find the number of characters with odd frequency
   for (int i = 0; i < 26; i++){
      if (fre[i] % 2){
         cnt += 1;
      }
   }
   
   // If characters with odd frequency are 0 or 1, then the string can be palindrome without any character deletion
   if (cnt == 0 || cnt == 1){
      return 0;
   }
   
   // Otherwise, return cnt - 1, as one character can be odd
   else{
      return cnt - 1;
   }
}
int main(){
   string str = "abcabccd";
   cout << "The minimum number of deletions of characters required to make string palindrome is - " << deletionCount(str) << endl;
}

Output

The minimum number of deletions of characters required to make string palindrome is - 1

Time complexity − O(N), as we count the frequency of each character in the given string.

Space complexity − O(1), as we use constant space.

Conclusion

We learned to find the minimum characters removal require to arrange the remaining characters in the palindromic string. We use the ‘freq’ array to store the frequency of each character, but users can also use the count() method to count the frequency of a particular character in the given string.

Updated on: 28-Jul-2023

531 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements