Check if a string can be rearranged to form special palindrome | Java 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
public class Improve { public static void main ( String args [ ] ) { String s = " ABCdcba " ; int u [ ] = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Character . isUpperCase ( s . charAt ( i ) ) ) { u [ s . charAt ( i ) - 65 ] += 1 ; } else { u [ s . charAt ( i ) - 97 ] -= 1 ; } } boolean 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 ) System . out . println ( " YES " ) ; else if ( po == 1 && ne == 0 ) System . out . println ( " YES " ) ; else if ( po == 0 && ne == - 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }