Rearrange a string in sorted order followed by the integer sum | Javascript program for above implementation ; Method to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver method
let MAX_CHAR = 26 ; function arrangeString ( str ) { let char_count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { char_count [ i ] = 0 ; } let sum = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= " " && str [ i ] <= " " ) char_count [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; else sum = sum + ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } let res = " " ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { let ch = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; while ( char_count [ i ] -- != 0 ) res = res + ch ; } if ( sum > 0 ) res = res + sum ; return res ; } let str = " " ; document . write ( arrangeString ( str ) ) ;