Given a string **S **of length **N **and an array **Q[][]** of queries in the form **{l, r, y}**. For each query, the task is to print the number of characters **y **present in the range **[l, r]**.

**Examples:**

Input:S = “aabv”, Q[][] = {{0, 3, ‘a’}, {1, 2, ‘b’}}Output:2 1Explanation:

Query 1: Number of character ‘a’ present in the range [0, 3] is 2.

Query 2: Number of character ‘b’ present in the range [1, 2] is 1.

Input:S = “abcd”, Q[][] = {{1, 3, ‘c’}, {1, 1, ‘b’}}Output:1 1Explanation:

Query 1: Number of character ‘c’ present in the range [1, 3] is 1.

Query 2: Number of character ‘b’ present in the range [1, 1] is 1.

**Naive Approach:** The simplest approach is to traverse the string over the range **[l, r]** and increment the counter by **1 **if the character at index **i **is equal to **y **for each query** {l, r, y}**. After traversing, print the counter for each query.

**Time Complexity:** O(N*Q)**Auxiliary Space:** O(N)

**Efficient Approach:** The idea is to pre-compute the number of characters present in the range **1 to i **for each character from **‘a’** to **‘z’** where **1 ≤ i ≤ N** using Prefix Sum technique. Follow the steps below to solve the problem:

- Initialize an array
**dp[N+1][26]**where**dp[i][j]**stores the number of characters**(i+’a’)**present in the range**[0, i]**. - Now, Precompute the number of each character present in the range
**[1, i]**where**1 ≤ i ≤ N**by incrementing**dp[i][j]**by**dp[i – 1][j]**where**0 ≤ j < 26**and increment**dp[i][S[j] – ‘a’]**by**1**. - For each query
**{l, r, y}**, print the value of**dp[r][y – ‘a’] – dp[l – 1][y – ‘a’]**as the number of characters**y**present in the range**[l, r]**.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to print count of char ` ` ` `// y present in the range [l, r] ` ` ` `static` `void` `noOfChars(String s, ` ` ` `char` `[][] queries) ` ` ` `{ ` ` ` ` ` `// Length of the string ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Stores the precomputed results ` ` ` `int` `dp[][] = ` `new` `int` `[n + ` `1` `][` `26` `]; ` ` ` ` ` `// Iterate the given string ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// Increment dp[i][y-'a'] by 1 ` ` ` `dp[i + ` `1` `][s.charAt(i) - ` `'a'` `]++; ` ` ` ` ` `// Pre-compute ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) { ` ` ` `dp[i + ` `1` `][j] += dp[i][j]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Number of queries ` ` ` `int` `q = queries.length; ` ` ` ` ` `// Traverse each query ` ` ` `for` `(` `int` `i = ` `0` `; i < q; i++) { ` ` ` ` ` `int` `l = (` `int` `)queries[i][` `0` `]; ` ` ` `int` `r = (` `int` `)queries[i][` `1` `]; ` ` ` `int` `c = queries[i][` `2` `] - ` `'a'` `; ` ` ` ` ` `// Print the result for ` ` ` `// each query ` ` ` `System.out.print( ` ` ` `dp[r] - dp[l - ` `1` `] ` ` ` `+ ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Given string ` ` ` `String S = ` `"aabv"` `; ` ` ` ` ` `// Given Queries ` ` ` `char` `queries[][] ` ` ` `= ` `new` `char` `[][] { { ` `1` `, ` `2` `, ` `'a'` `}, ` ` ` `{ ` `2` `, ` `3` `, ` `'b'` `} }; ` ` ` ` ` `// Function Call ` ` ` `noOfChars(S, queries); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 1

**Time Complexity:** O(Q+N*26)**Auxiliary Space:** O(N)

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 number of indices such that s[i] = s[i+1] : Range queries
- Range Queries for Frequencies of array elements
- Array range queries over range queries
- Range sum queries for anticlockwise rotations of Array by K indices
- Queries to calculate the Sum of Array elements in the range [L, R] having indices as multiple of K
- Queries to count occurrences of maximum array element in subarrays starting from given indices
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Queries for frequencies of characters in substrings
- Queries to find frequencies of a string within specified substrings
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Queries to print the character that occurs the maximum number of times in a given range
- Queries for Nth smallest character for a given range in a string
- Queries to find total number of duplicate character in range L to R in the string S
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Find array elements with frequencies in range [l , r]
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Binary Indexed Tree : Range Update and Range Queries
- Smallest index in given range of indices which is not equal to X
- Count clockwise array rotations required to maximize count of array elements present at indices same as their value
- Count frequencies of all elements in array in Python using collections module

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.