Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of String ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code
def countSmaller ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE arr = [ 0 ] * 26 ; NEW_LINE ans = [ 0 ] * n ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT arr [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE ct = 0 ; NEW_LINE for j in range ( ord ( str [ i ] ) - ord ( ' a ' ) ) : NEW_LINE INDENT ct += arr [ j ] ; NEW_LINE DEDENT ans [ i ] = ct ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " edcbaa " ; NEW_LINE countSmaller ( str ) ; NEW_LINE DEDENT