Rearrange the string to maximize the number of palindromic substrings | Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver code
function newString ( s ) { let l = s . length ; let freq = new Array ( 26 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < l ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let ans = " " ; for ( let i = 0 ; i < 26 ; i ++ ) { for ( let j = 0 ; j < freq [ i ] ; j ++ ) { ans += String . fromCharCode ( 97 + i ) ; } } return ans ; } let s = " " ; document . write ( newString ( s ) ) ;