C++ Minimum Removal of Characters from string to make its permutation as palindrome














































C++ Minimum Removal of Characters from string to make its permutation as palindrome



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:

  1. Check if the given string is already a palindrome or not. If found to be true, print 0.
  2. Otherwise, calculate the frequency of each character in the string using a Hashmap.
  3. Count the number of characters with odd frequencies and store it in a variable, say k.
  4. Now, the total number of characters required to be deleted is k-1. Therefore, print k %u2013 1 as the required answer.

Below is the implementation of the above approach:

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a string
// is palindrome or not
bool 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 palindrome
void 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);
}
Output: 
1

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


More Articles of M Mounika:

Name Views Likes
C++ Segmented Sieve (Print Primes In a Range) 162 0
C++ Sieve Of Erastosthenes 135 0
C++ Gold Mine Problem 295 0
C++ Merge K Sorted Arrays 117 0
C++ K Centers Problem 240 0
C++ Find Nth Catalan Number 311 0
C++ Inplace Rotate square matrix by 90 degrees 285 0
C++ Find Non Repeating Elements in Array 87 0
C++ Merge Two Binary Trees 120 0
C++ Sum of Numbers From Root To Leaf Paths 89 0
C++ Meta Strings 91 0
C++ Flood Fill Algorithm 402 0
C++ smallest substring with maximum distinct characters 199 0
C++ Smallest window with all characters in string 93 0
C++ Minimum Removal of Characters from string to make its permutation as palindrome 87 0
C++ Minimum characters added at front of string in palindrome conversion 69 0
C++ Number of Bracket Reversals needed to make expression Balanced 72 0
C++ String to Palindrome with Append Function 83 0
C++ WildCard pattern matching 75 0
C++ Anagram substring Search 72 0
C++ Manachars Algorithm 74 0
C++ Search String in Grid 83 0
C++ String Matching(Z Algorithm) 67 0
C++ String Matching(Naive Algorithm) 113 0
C++ String Matching(KMP Algorithm) 140 0
C++ Remove Duplicates From String 110 0
C++ Basics of String Manipulation 85 1
C++ Disjoint Data Structure Cycle Detection 86 0
C++ Problem On Disjoint Data Structures 95 0
C++ Disjoint Data Structures Part3 79 0
Disjoint Data Structures Part2 90 0
Disjoint Data Structures 93 1
C++ Segment Trees 321 2
C++ Trie Cost of Data 290 1
C++ Trie Datastructure 279 1
C++ Greedy Approach Minimum number of coins 525 0
C++ Greedy Approach Maximum height Pyramid 328 1
C++ Greedy Approach String lexicographically largest subsequence 246 0
C++ Greedy Approach Lexicographically largest subsequence 364 0
C++ Greedy Approach Prims MST 398 1
C++ Greedy Approach Krushkals MST 458 1
C++ Greedy Approach N-array maximum sum 333 1
C++ Greedy Approach Policemen Catch Thieves 563 1
C++ Greedy Approach Maximum product Subset 546 1
C++ Greedy Approach Minimum Product Subset 348 1
C++ Greedy Approach Fractional Knapsack 737 1
C++ Greedy Approach-Activity Selection Problem 745 1
C++ Greedy Approach-Egyptian Fractions 639 0
C++ Greedy Approach-Huffman Codes 1031 1
C++ Introduction to Greedy Approach 955 2

Comments