Queries for frequencies of characters in substrings | C # program to find occurrence of character in substring l to r ; To store count of all character ; To pre - process string from 0 to size of string ; Store occurrence of character i ; Store occurrence o all character upto i ; To return occurrence of character in range l to r ; Return occurrence of character from 0 to r minus its occurrence from 0 to l ; Driver code
using System ; class GFG { static int MAX_LEN = 1005 ; static int MAX_CHAR = 26 ; static int [ , ] cnt = new int [ MAX_LEN , MAX_CHAR ] ; static void preProcess ( string s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) cnt [ i , s [ i ] - ' a ' ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) cnt [ i , j ] += cnt [ i - 1 , j ] ; } } static int findCharFreq ( int l , int r , char c ) { return ( cnt [ r , c - 97 ] - cnt [ l , c - 97 ] ) ; } public static void Main ( string [ ] args ) { string s = " geeksforgeeks " ; int Q = 4 ; preProcess ( s ) ; Console . WriteLine ( findCharFreq ( 0 , 5 , ' e ' ) ) ; Console . WriteLine ( findCharFreq ( 2 , 6 , ' f ' ) ) ; Console . WriteLine ( findCharFreq ( 4 , 7 , ' m ' ) ) ; Console . WriteLine ( findCharFreq ( 0 , 12 , ' e ' ) ) ; } }