Count of lexicographically smaller characters on right | function to count the smaller characters at the right of index i ; store the length of string ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; input string
function countSmaller ( str ) { var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) { var cnt = 0 ; for ( var j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] < str [ i ] ) { cnt += 1 ; } } document . write ( cnt + " " ) ; } } var str = " " ; countSmaller ( str ) ;