Given an array of strings **arr[]** and two integers **l** and **r**, the task is to find the number of times the given string **str** occurs in the array in the range **[l, r]** (1-based indexing). **Note** that the strings contain only lowercase letters.

**Examples:**

Input:arr[] = {“abc”, “def”, “abc”}, L = 1, R = 2, str = “abc”

Output:1

Input:arr[] = {“abc”, “def”, “abc”}, L = 1, R = 3, str = “ghf”

Output:0

**Approach:** The idea is to use an unordered_map to store the indices in which the ith string of array occurs. If the given string is not present in the map then answer is zero otherwise perform binary search on the indices of the given string present in the map, and find the number of occurrences of the string in the range [L, R].

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of occurrences of ` `int` `NumOccurrences(string arr[], ` `int` `n, string str, ` `int` `L, ` `int` `R) ` `{ ` ` ` `// To store the indices of strings in the array ` ` ` `unordered_map<string, vector<` `int` `> > M; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `string temp = arr[i]; ` ` ` `auto` `it = M.find(temp); ` ` ` ` ` `// If current string doesn't ` ` ` `// have an entry in the map ` ` ` `// then create the entry ` ` ` `if` `(it == M.end()) { ` ` ` `vector<` `int` `> A; ` ` ` `A.push_back(i + 1); ` ` ` `M.insert(make_pair(temp, A)); ` ` ` `} ` ` ` `else` `{ ` ` ` `it->second.push_back(i + 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `auto` `it = M.find(str); ` ` ` ` ` `// If the given string is not ` ` ` `// present in the array ` ` ` `if` `(it == M.end()) ` ` ` `return` `0; ` ` ` ` ` `// If the given string is present ` ` ` `// in the array ` ` ` `vector<` `int` `> A = it->second; ` ` ` `int` `y = upper_bound(A.begin(), A.end(), R) - A.begin(); ` ` ` `int` `x = upper_bound(A.begin(), A.end(), L - 1) - A.begin(); ` ` ` ` ` `return` `(y - x); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"abc"` `, ` `"abcabc"` `, ` `"abc"` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(string); ` ` ` `int` `L = 1; ` ` ` `int` `R = 3; ` ` ` `string str = ` `"abc"` `; ` ` ` ` ` `cout << NumOccurrences(arr, n, str, L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` `from` `bisect ` `import` `bisect_right as upper_bound ` `from` `collections ` `import` `defaultdict ` ` ` `# Function to return the number of occurrences of ` `def` `numOccurences(arr: ` `list` `, n: ` `int` `, string: ` `str` `, L: ` `int` `, R: ` `int` `) ` `-` `> ` `int` `: ` ` ` ` ` `# To store the indices of strings in the array ` ` ` `M ` `=` `defaultdict(` `lambda` `: ` `list` `) ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `temp ` `=` `arr[i] ` ` ` ` ` `# If current string doesn't ` ` ` `# have an entry in the map ` ` ` `# then create the entry ` ` ` `if` `temp ` `not` `in` `M: ` ` ` `A ` `=` `[] ` ` ` `A.append(i ` `+` `1` `) ` ` ` `M[temp] ` `=` `A ` ` ` `else` `: ` ` ` `M[temp].append(i ` `+` `1` `) ` ` ` ` ` `# If the given string is not ` ` ` `# present in the array ` ` ` `if` `string ` `not` `in` `M: ` ` ` `return` `0` ` ` ` ` `# If the given string is present ` ` ` `# in the array ` ` ` `A ` `=` `M[string] ` ` ` `y ` `=` `upper_bound(A, R) ` ` ` `x ` `=` `upper_bound(A, L ` `-` `1` `) ` ` ` ` ` `return` `(y ` `-` `x) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[` `"abc"` `, ` `"abcabc"` `, ` `"abc"` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `L ` `=` `1` ` ` `R ` `=` `3` ` ` `string ` `=` `"abc"` ` ` ` ` `print` `(numOccurences(arr, n, string, L, R)) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Queries to print the character that occurs the maximum number of times in a given range
- Queries to check whether all the elements in the given range occurs even number of times
- Count of Numbers in a Range where digit d occurs exactly K times
- Element which occurs consecutively in a given subarray more than or equal to K times
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence where each character occurs at least k times
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Maximum length prefix of one string that occurs as subsequence in another
- Check if the string contains consecutive letters and each letter occurs exactly once
- Sort elements of the array that occurs in between multiples of K
- Check if given array can be made 0 with given operations performed any number of times
- Enlarge a Matrix such that each element occurs in R rows and C columns
- C++ program to concatenate a string given number of times
- Repeat substrings of the given String required number of times
- String Range Queries to find the number of subsets equal to a given String
- Maximum subarray sum in array formed by repeating the given array k times
- Given an array of size n and a number k, find all elements that appear more than n/k times

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.