Number of Larger Elements on right side in a string | Javascript program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code
let MAX_CHAR = 26 ; function printGreaterCount ( str ) { let len = str . length ; let ans = new Array ( len ) ; ans . fill ( 0 ) ; let count = new Array ( MAX_CHAR ) ; count . fill ( 0 ) ; for ( let i = len - 1 ; i >= 0 ; i -- ) { count [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( let j = str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( let i = 0 ; i < len ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let str = " " ; printGreaterCount ( str ) ;