Given a string s and Q number of queries. Each query Q consists of l and r and a character c. Find frequency of character c in substring l to r.
Input : s = geeksforgeeks 4 0 5 e 2 6 f 4 7 m 0 12 e Output : 2 1 0 4 Substring from 0 to 5 is geeksf. Here e occurs 2 times. Input : s = apple 2 0 4 e 1 2 p Output : 1 2
Naive Approach: Run a loop from l to r for Q number of queries. Count occurrence of character and return count. Overall time complexity will be Q * O(|s|).
Efficient Approach:We can pre-compute the count for each character. Store count of each character in 2-D array. Return frequency of character from 0 to r minus frequency of character in range 0 to l in O(1). Overall time complexity will be Q * O(1).
2 1 0 4
- Range Queries for Frequencies of array elements
- XOR of Prime Frequencies of Characters in a String
- Print characters and their frequencies in order of occurrence
- Print characters having even frequencies in order of occurrence
- Check whether the frequencies of all the characters in a string are prime or not
- Print characters having odd frequencies in order of occurrence
- Sum and Product of Prime Frequencies of Characters in a String
- Subsequence queries after removing substrings
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Print characters having prime frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Queries to find the count of vowels in the substrings of the given string
- Count substrings with same first and last characters
- Count distinct substrings that contain some characters at most 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 firstname.lastname@example.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.