
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum removal to make palindrome permutation in C++
Problem statement
Given a string S, we have to find minimum characters that we can remove to make any permutation of the string S a palindrome
Example
If str = “abcdba” then we remove to 1 character i.e. either ‘c’ or ‘d’.
Algorithms
1. There can be two types of a palindrome, even length, and odd length palindromes 2. We can deduce the fact that an even length palindrome must have every character occurring even number of times 3.An odd palindrome must have every character occurring even number of times except one character occurring odd number of time 4. Check the frequency of every character and those characters occurring odd number of times are then counted. Then the result is total count of odd frequency characters’ minus 1
Example
#include <bits/stdc++.h> #define MAX 26 using namespace std; int minCharactersRemoved(string str) { int hash[MAX] = {0}; for (int i = 0; str[i]; ++i) { hash[str[i] - 'a']++; } int cnt = 0; for (int i = 0; i < MAX; ++i) { if (hash[i] & 1) { ++cnt; } } return (cnt == 0) ? 0 : (cnt - 1); } int main(){ string str = "abcdba"; cout << "Minimum characters to be removed = " << minCharactersRemoved(str) << endl; return 0; }
When you compile and execute above program. It generates following output
Output
Minimum characters to be removed = 1
- Related Articles
- Palindrome Removal on C++
- Palindrome Permutation II in C++
- Minimum Insertion Steps to Make a String Palindrome in C++
- Minimum number of deletions to make a string palindrome in C++.
- Minimum number of Appends needed to make a string palindrome in C++
- Find minimum number of merge operations to make an array palindrome in C++
- Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Checking for permutation of a palindrome in JavaScript
- Maximum edge removal from tree to make even forest in C++
- Program to find minimum number of characters to be added to make it palindrome in Python
- Maximum even length sub-string that is permutation of a palindrome in C++
- Minimum Remove to Make Valid Parentheses in C++
- Minimum Swaps to Make Strings Equal in C++

Advertisements