Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • 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: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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:

 

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++




// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of deletions
// required such that characters of the
// string can be rearranged to form a palindrome
int minDeletions(string str)
{
    // Stores frequency of characters
    int fre[26];
    memset(fre, 0, sizeof(fre));
 
    int n = str.size();
cout<<n;
    // Store the frequency of each
    // character in frequency array
    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;
 
    // Count number of characters
    // with odd frequency
    for (int i = 0; i < 26; i++) {
        if (fre[i] % 2) {
            count += 1;
        }
    }
 
    // If count is 1 or 0, return 0
    if (count == 0 || count == 1) {
        return 0;
    }
 
    // Otherwise, return count - 1
    else {
        return count - 1;
    }
}
 
// Driver Code
int main()
{
    string str = "ababbccca";
 
    // Function call to find minimum
    // number of deletions required
    cout << minDeletions(str) << endl;
}

Java




// Java program for the above approach
public class GFG
{
 
  // Function to find the number of deletions
  // required such that characters of the
  // string can be rearranged to form a palindrome
  static int minDeletions(String str)
  {
    // Stores frequency of characters
    int fre[] = new int[26];
 
    int n = str.length();
 
    // Store the frequency of each
    // character in frequency array
    for (int i = 0; i < n; i++) {
      fre[str.charAt(i) - 'a'] += 1;
    }
 
    int count = 0;
 
    // Count number of characters
    // with odd frequency
    for (int i = 0; i < 26; i++) {
      if (fre[i] % 2 == 1) {
        count += 1;
      }
    }
 
    // If count is 1 or 0, return 0
    if (count == 0 || count == 1) {
      return 0;
    }
 
    // Otherwise, return count - 1
    else {
      return count - 1;
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    String str = "ababbccca";
 
    // Function call to find minimum
    // number of deletions required
    System.out.println(minDeletions(str)) ;
  }
}
 
// This code is contributed by AnkThon

Python3




# Python3 program for
# the above approach
 
# Function to find the number of deletions
# required such that characters of the
# string can be rearranged to form a palindrome
def minDeletions(str):
     
    # Stores frequency of characters
    fre = [0]*26
     
    # memset(fre, 0, sizeof(fre));
    n = len(str)
 
    # Store the frequency of each
    # character in frequency array
    for i in range(n):
        fre[ord(str[i]) - ord('a')] += 1
 
    count = 0
 
    # Count number of characters
    # with odd frequency
    for i in range(26):
        if (fre[i] % 2):
            count += 1
 
    # If count is 1 or 0, return 0
    if (count == 0 or count == 1):
        return 0
 
    # Otherwise, return count - 1
    else:
        return count - 1
 
# Driver Code
if __name__ == '__main__':
    str = "ababbccca"
 
    # Function call to find minimum
    # number of deletions required
    print (minDeletions(str))
 
    # This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the number of deletions
  // required such that characters of the
  // string can be rearranged to form a palindrome
  static int minDeletions(string str)
  {
 
    // Stores frequency of characters
    int []fre = new int[26];
    int n = str.Length;
 
    // Store the frequency of each
    // character in frequency array
    for (int i = 0; i < n; i++)
    {
      fre[str[i] - 'a'] += 1;
    }
 
    int count = 0;
 
    // Count number of characters
    // with odd frequency
    for (int i = 0; i < 26; i++) {
      if (fre[i] % 2 == 1) {
        count += 1;
      }
    }
 
    // If count is 1 or 0, return 0
    if (count == 0 || count == 1) {
      return 0;
    }
 
    // Otherwise, return count - 1
    else {
      return count - 1;
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string str = "ababbccca";
 
    // Function call to find minimum
    // number of deletions required
    Console.WriteLine(minDeletions(str)) ;
  }
}
 
// This code is contributed by AnkThon

Javascript




<script>
//Javascript program for
// the above approach
 
// Function to find the number of deletions
// required such that characters of the
// string can be rearranged to form a palindrome
function minDeletions(str)
{
    // Stores frequency of characters
    var fre = new Array(26);
    fre.fill(0);
 
    var n = str.length;
 
    // Store the frequency of each
    // character in frequency array
    for (var i = 0; i < n; i++) {
        //let a = parseInt(str[i] - 97);
        fre[str[i].charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
         
    }
    var count = 0;
 
    // Count number of characters
    // with odd frequency
    for (var i = 0; i < 26; i++) {
        if (fre[i] % 2) {
            count += 1;
        }
    }
 
    // If count is 1 or 0, return 0
    if (count == 0 || count == 1) {
        return 0;
    }
 
    // Otherwise, return count - 1
    else {
        return count - 1;
    }
}
 
    var str = "ababbccca";
 
    // Function call to find minimum
    // number of deletions required
    document.write( minDeletions(str)+"<br>");
     
     
    // This code is contributed by SoumikMondal
</script>
Output: 
2

 

Time Complexity: O(N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :