Number of Larger Elements on right side in a string | Java 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
public class GFG { final static int MAX_CHAR = 26 ; static void printGreaterCount ( String str ) { int len = str . length ( ) ; int ans [ ] = new int [ len ] , count [ ] = new int [ MAX_CHAR ] ; for ( int i = len - 1 ; i >= 0 ; i -- ) { count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int j = str . charAt ( i ) - ' a ' + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }