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: 1Input: 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; } |
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 |
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:
- Queries to check whether all the elements in the given range occurs even number of times
- Queries to print the character that occurs the maximum number of times in a given range
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Count of Numbers in a Range where digit d occurs exactly K times
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Longest subsequence where each character occurs at least 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
- Number of subsequences as "ab" in a string repeated K times
- 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
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- First element that appears even number of times in an array
- GCD of elements occurring Fibonacci number of times in an Array
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Number of times Maximum and minimum value updated during traversal of array
- Find the number of different numbers in the array after applying the given operation q times
- Check if given array can be made 0 with given operations performed any number of 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.
Improved By : sanjeev2552

