Make palindromic string non | 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
function findNonPalinString ( s ) { var freq = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var flag = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( flag == 0 ) document . write ( " " ) ; else { for ( var i = 0 ; i < 26 ; i ++ ) for ( var j = 0 ; j < freq [ i ] ; j ++ ) document . write ( String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ) ; } } var s = " " ; findNonPalinString ( s . split ( ' ' ) ) ;