Queries to find frequencies of a string within specified substrings | Javascript 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
var max_len = 100005 ; var cnt = Array ( max_len ) . fill ( 0 ) ; function precompute ( s , K ) { var n = s . length ; for ( var i = 0 ; i < n - 1 ; i ++ ) { cnt [ i + 1 ] = cnt [ i ] + ( s . substring ( i , i + K . length ) == K ) ; } } var s = " " ; var K = " " ; precompute ( s , K ) ; var Q = [ [ 1 , 6 ] , [ 5 , 11 ] ] ; Q . forEach ( ( it ) => { document . write ( cnt [ it [ 1 ] - 1 ] - cnt [ it [ 0 ] - 1 ] + " " ) ; } ) ;