Check if a string can be rearranged to form special palindrome | C ++ implementation of the above approach ; Driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { string s = " ABCdcba " ; int u [ 26 ] = { 0 } ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isupper ( s [ i ] ) ) { u [ s [ i ] - 65 ] += 1 ; } else { u [ s [ i ] - 97 ] -= 1 ; } } bool f1 = true ; int po = 0 ; int ne = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) cout << ( " YES " ) << endl ; else if ( po == 1 && ne == 0 ) cout << ( " YES " ) << endl ; else if ( po == 0 && ne == -1 ) cout << ( " YES " ) << endl ; else cout << ( " NO " ) << endl ; }