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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if a string// is palindrome or notbool IsPalindrome(string& str){ string s = str; // Reverse the string reverse(str.begin(), str.end()); // Check if the string is // already a palindrome or not if (s == str) { return true; } return false;}// Function to calculate the minimum// deletions to make a string palindromevoid CountDeletions(string& str, int len){ if (IsPalindrome(str)) { cout << 0 << endl; return; } // Stores the frequencies // of each character map<char, int> mp; // Iterate over the string for (int i = 0; i < len; i++) { // Update frequency of // each character mp[str[i]]++; } int k = 0; // Iterate over the map for (auto it : mp) { // Count characters with // odd frequencies if (it.second & 1) { k++; } } // Print the result cout << k - 1 << endl;}int main(){ string str = "abca"; int len = str.length(); CountDeletions(str, len);} |
Java
// Java program for the// above approachimport 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);} // Function to check if a String// is palindrome or notstatic boolean IsPalindrome(){ String s = str; // Reverse the String s = reverse(str); // Check if the String is // already a palindrome or not if (s == str) { return true; } return false;}// Function to calculate the// minimum deletions to make// a String palindromestatic void CountDeletions(int len){ if (IsPalindrome()) { System.out.print(0 + "\n"); return; } // Stores the frequencies // of each character HashMap<Character, Integer> mp = new HashMap<>(); // Iterate over the String for (int i = 0; i < len; i++) { // Update frequency of // each character 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; // Iterate over the map for (Map.Entry<Character, Integer> it : mp.entrySet()) { // Count characters with // odd frequencies if (it.getValue() % 2 == 1) { k++; } } // Print the result System.out.print(k - 1 + "\n");}// Driver codepublic static void main(String[] args){ str = "abca"; int len = str.length(); CountDeletions(len);}}// This code is contributed by gauravrajput1 |
Python3
# Python3 program for the above approachfrom collections import defaultdict# Function to check if a string# is palindrome or notdef IsPalindrome(str): s = str # Reverse the string s = s[::-1] # Check if the string is # already a palindrome or not if (s == str): return True return False# Function to calculate the minimum# deletions to make a string palindromedef CountDeletions(str, ln): if (IsPalindrome(str)): print(0) return # Stores the frequencies # of each character mp = defaultdict(lambda : 0) # Iterate over the string for i in range(ln): # Update frequency of # each character mp[str[i]] += 1 k = 0 # Iterate over the map for it in mp.keys(): # Count characters with # odd frequencies if (mp[it] & 1): k += 1 # Print the result print(k - 1)# Driver codeif __name__ == '__main__': str = "abca" ln = len(str) # Function Call CountDeletions(str, ln)# This code is contributed by Shivam Singh |
C#
// C# program for the// above approachusing 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);} // Function to check if// a String is palindrome // or notstatic bool IsPalindrome(){ String s = str; // Reverse the String s = reverse(str); // Check if the String // is already a palindrome // or not if (s == str) { return true; } return false;}// Function to calculate the// minimum deletions to make// a String palindromestatic void CountDeletions(int len){ if (IsPalindrome()) { Console.Write(0 + "\n"); return; } // Stores the frequencies // of each character Dictionary<char, int> mp = new Dictionary<char, int>(); // Iterate over the String for (int i = 0; i < len; i++) { // Update frequency of // each character if(mp.ContainsKey(str[i])) { mp[str[i]] = mp[str[i]] + 1; } else { mp.Add(str[i], 1); } } int k = 0; // Iterate over the map foreach (KeyValuePair<char, int> it in mp) { // Count characters with // odd frequencies if (it.Value % 2 == 1) { k++; } } // Print the result Console.Write(k - 1 + "\n");}// Driver codepublic static void Main(String[] args){ str = "abca"; int len = str.Length; CountDeletions(len);}}// This code is contributed by Princi Singh |
1
Time Complexity: O(N)
Auxiliary Space: O(N)
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.



