Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; Difference between max and min ; Driver Code ; Given string ; Given queries ; Function Call
def maxDiffFreq ( queries , S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE Q = len ( queries ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT l = queries [ i ] [ 0 ] - 1 NEW_LINE r = queries [ i ] [ 1 ] - 1 NEW_LINE freq = [ 0 ] * 26 NEW_LINE for j in range ( l , r + 1 ) : NEW_LINE INDENT freq [ ord ( S [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT mx = 0 NEW_LINE mn = 99999999 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT mx = max ( mx , freq [ j ] ) NEW_LINE if ( freq [ j ] ) : NEW_LINE INDENT mn = min ( mn , freq [ j ] ) NEW_LINE DEDENT DEDENT print ( mx - mn ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abaabac " NEW_LINE queries = [ [ 2 , 6 ] , [ 1 , 7 ] ] NEW_LINE maxDiffFreq ( queries , S ) NEW_LINE DEDENT