Queries to count frequencies of a given character in a given range of indices | Function to print count 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 ; Given string ; Given Queries ; Function Call
function noOfChars ( s , queries ) { var n = s . length ; var dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( 26 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { dp [ i + 1 ] [ s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 ] [ j ] += dp [ i ] [ j ] ; } } var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) { var l = String . fromCharCode ( queries [ i ] [ 0 ] ) . charCodeAt ( 0 ) ; var r = String . fromCharCode ( queries [ i ] [ 1 ] ) . charCodeAt ( 0 ) ; var c = queries [ i ] [ 2 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; document . write ( dp [ r ] - dp [ l - 1 ] + " " ) ; } } var S = " " ; var queries = [ [ 1 , 2 , ' ' ] , [ 2 , 3 , ' ' ] ] ; noOfChars ( S , queries ) ;