Queries to count frequencies of a given character in a given range of indices | Function to prcount of char y present in the range [ l , r ] ; Length of the string ; Stores the precomputed results ; Iterate the given string ; Increment dp [ i ] [ y - ' a ' ] by 1 ; Pre - compute ; Number of queries ; Traverse each query ; Print the result for each query ; Driver Code ; Given string ; Given Queries ; Function Call
def noOfChars ( s , queries ) : NEW_LINE INDENT n = len ( s ) NEW_LINE dp = [ [ 0 for i in range ( 26 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i + 1 ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT dp [ i + 1 ] [ j ] += dp [ i ] [ j ] NEW_LINE DEDENT DEDENT q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT l = queries [ i ] [ 0 ] NEW_LINE r = queries [ i ] [ 1 ] NEW_LINE c = ord ( queries [ i ] [ 2 ] ) - ord ( ' a ' ) NEW_LINE print ( dp [ r ] - dp [ l - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabv " NEW_LINE queries = [ [ 1 , 2 , ' a ' ] , [ 2 , 3 , ' b ' ] ] NEW_LINE noOfChars ( S , queries ) NEW_LINE DEDENT