Make palindromic string non | Java Program to rearrange letters of string to find a non - palindromic string if it exists ; Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code
class GfG { static void findNonPalinString ( char s [ ] ) { int freq [ ] = new int [ 26 ] ; int flag = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( flag == 0 ) System . out . println ( " - 1" ) ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) System . out . print ( ( char ) ( ' a ' + i ) ) ; } } public static void main ( String [ ] args ) { String s = " abba " ; findNonPalinString ( s . toCharArray ( ) ) ; } }