Number of Larger Elements on right side in a string | PHP 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
< ? php $ MAX_CHAR = 26 ; function printGreaterCount ( $ str ) { global $ MAX_CHAR ; $ len = strlen ( $ str ) ; $ ans = array_fill ( 0 , $ len , 0 ) ; $ count = array_fill ( 0 , $ MAX_CHAR , 0 ) ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ j = ord ( $ str [ $ i ] ) - ord ( ' a ' ) + 1 ; $ j < $ MAX_CHAR ; $ j ++ ) $ ans [ $ i ] += $ count [ $ j ] ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ ans [ $ i ] . " ▁ " ; } $ str = " abcd " ; printGreaterCount ( $ str ) ; ? >