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
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.
- Queries to find frequencies of a string within specified substrings
- Range Queries for Frequencies of array elements
- XOR of Prime Frequencies of Characters in a String
- Print characters having even frequencies in order of occurrence
- Print characters and their 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
- 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
- Binary string with given frequencies of sums of consecutive pairs of characters
- Print characters and their frequencies in order of occurrence using Binary Tree
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Subsequence queries after removing substrings
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Queries to find the count of vowels in the substrings of the given string
- Count substrings with same first and last characters
- Count of substrings formed using a given set of characters only
- Count of substrings having all distinct characters