Print all positions of a given string having count of smaller characters equal on both sides | Function to find indexes of the given that satisfy the condition ; Stores length of given string ; Stores frequency of each character of strr ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given string ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ strr [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; Driver Code
def printIndexes ( strr ) : NEW_LINE INDENT N = len ( strr ) NEW_LINE cntFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntFreq [ ord ( strr [ i ] ) ] += 1 NEW_LINE DEDENT cntLeftFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntLeft = 0 NEW_LINE cntRight = 0 NEW_LINE for j in range ( ord ( strr [ i ] ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT cntLeft += cntLeftFreq [ j ] NEW_LINE cntRight += ( cntFreq [ j ] - cntLeftFreq [ j ] ) NEW_LINE DEDENT cntLeftFreq [ ord ( strr [ i ] ) ] += 1 NEW_LINE if ( cntLeft == cntRight and cntLeft != 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " aabacdabbb " NEW_LINE printIndexes ( strr ) NEW_LINE DEDENT