Queries to find frequencies of a string within specified substrings | C # program to find frequency of a string K in a substring [ L , R ] in S ; Store the frequency of string for each index ; Compute and store frequencies for every index ; Driver code
using System . IO ; using System ; class GFG { static int max_len = 100005 ; static int [ ] cnt = new int [ max_len ] ; static void precompute ( string s , string K ) { int n = s . Length ; for ( int i = 0 ; i < n - 2 ; i ++ ) { cnt [ i + 1 ] = cnt [ i ] ; if ( s . Substring ( i , K . Length ) . Equals ( K ) ) { cnt [ i + 1 ] += 1 ; } } cnt [ n - 2 + 1 ] = cnt [ n - 2 ] ; } static void Main ( ) { string s = " ABCABCABABC " ; string K = " ABC " ; precompute ( s , K ) ; int [ , ] Q = { { 1 , 6 } , { 5 , 11 } } ; for ( int it = 0 ; it < Q . GetLength ( 0 ) ; it ++ ) { Console . WriteLine ( cnt [ Q [ it , 1 ] - 1 ] - cnt [ Q [ it , 0 ] - 1 ] ) ; } } }