Given a string **S** and a matrix **Q** of queries, each specifying the starting and ending indices **L( = Q[i][0])** and **R( = Q[i][0])** respectively of a substring of S, the task is to find the frequency of string **K** in substring **[L, R]**.

**Note:** The ranges follow the 1-based indexing.

**Examples:**

Input:S = “GFGFFGFG”, K = “GFG”, Q = {{1, 8}, {3, 5}, {5, 8}}

Output:

2

0

1

Explanation:For query 1, there are 2 (“GFG”) substrings from index 1 to index 8. One is from index 1 to 3 and the other is from index 6 to 8.

For query 2, there are 0 (“GFG”) substrings from index 3 to 5.

For query 3, there are 1 (“GFG”) substrings from index 5 to index 8. The one and only substring are from index 6 to 8.

Input:S = “ABCABCABABC”, K = “ABC”, Q = {{1, 6}, {5, 11}}

Output:

2

1

**Naive Approach:**

Run a loop from **L** to **R** for all the queries. Count occurrence of the string **K** and return count.

**Time Complexity:** O(length of Q * |S|).

**Efficient Approach: **

Pre-compute and store the frequency of K for every index. Now, for computing the frequency of the string in a range **[L, R]**, we just need to calculate the difference between the frequency of K at index **(R-1)** and **(L-1)**.

Below is the implementation of the above approach:

## C++

`// C++ Program to find ` `// frequency of a string K ` `// in a substring [L, R] in S ` ` ` `#include <bits/stdc++.h> ` `#define max_len 100005 ` `using` `namespace` `std; ` ` ` `// Store the frequency of ` `// string for each index ` `int` `cnt[max_len]; ` ` ` `// Compute and store frequencies ` `// for every index ` `void` `precompute(string s, string K) ` `{ ` ` ` ` ` `int` `n = s.size(); ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `cnt[i + 1] ` ` ` `= cnt[i] ` ` ` `+ (s.substr(i, 3) == K); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"ABCABCABABC"` `; ` ` ` `string K = ` `"ABC"` `; ` ` ` `precompute(s, K); ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > Q ` ` ` `= { { 1, 6 }, { 5, 11 } }; ` ` ` ` ` `for` `(` `auto` `it : Q) { ` ` ` `cout << cnt[it.second - 1] ` ` ` `- cnt[it.first - 1] ` ` ` `<< endl; ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 1

**Time Complexity:** *O( | S | + length of Q )*, as every query is answered in O(1).

**Auxillary Space:** *O( |S| )*

## Recommended Posts:

- Queries for frequencies of characters in substrings
- Queries to find the count of vowels in the substrings of the given string
- String Range Queries to find the number of subsets equal to a given String
- Range Queries for Frequencies of array elements
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Subsequence queries after removing substrings
- Queries to find total number of duplicate character in range L to R in the string S
- Find distinct characters in distinct substrings of a string
- XOR of Prime Frequencies of Characters in a String
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Generate a string with maximum possible alphabets with odd frequencies
- Sum and Product of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Binary string with given frequencies of sums of consecutive pairs of characters
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Find array elements with frequencies in range [l , r]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.