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