Queries to count frequencies of a given character in a given range of indices | C # program for the above approach ; 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 ; Driver Code ; Given string ; Given Queries ; Function Call
using System ; public class GFG { static void noOfChars ( String s , char [ , ] queries ) { int n = s . Length ; int [ , ] dp = new int [ n + 1 , 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i + 1 , s [ i ] - ' a ' ] ++ ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 , j ] += dp [ i , j ] ; } } int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = ( int ) queries [ i , 0 ] ; int r = ( int ) queries [ i , 1 ] ; int c = queries [ i , 2 ] - ' a ' ; Console . Write ( dp [ r , c ] - dp [ l - 1 , c ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String S = " aabv " ; char [ , ] queries = new char [ , ] { { ( char ) 1 , ( char ) 2 , ' a ' } , { ( char ) 2 , ( char ) 3 , ' b ' } } ; noOfChars ( S , queries ) ; } }