# Queries for frequencies of characters in substrings

• Difficulty Level : Medium
• Last Updated : 06 Oct, 2021

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.
Examples:

```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 ```

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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).

## C++

 `// CPP program to find occurrence``// of character in substring l to r``#include ``#define MAX_LEN 1005``#define MAX_CHAR 26` `using` `namespace` `std;` `// To store count of all character``int` `cnt[MAX_LEN][MAX_CHAR];` `// To pre-process string from``// 0 to size of string``void` `preProcess(string s)``{``    ``int` `n = s.length();` `    ``// Initializing cnt with 0``    ``memset``(cnt, 0, ``sizeof``(cnt));` `    ``// Store occurrence of``    ``// character i``    ``for` `(``int` `i = 0; i < n; i++)``        ``cnt[i][s[i] - ``'a'``]++;` `    ``// Store occurrence o``    ``// all character upto i``    ``for` `(``int` `i = 1; i < n; i++) {``        ``for` `(``int` `j = 0; j < 26; j++)``            ``cnt[i][j] += cnt[i - 1][j];``    ``}``}` `// To return occurrence of``// character in range l to r``int` `findCharFreq(``int` `l, ``int` `r, ``char` `c)``{``    ``// Return occurrence of character``    ``// from 0 to r minus its``    ``// occurrence from 0 to l``    ``int` `count = cnt[r][c-``'a'``];``  ` `    ``if` `(l != 0)``        ``count -= cnt[l - 1][c-``'a'``];``    ``return` `count;``}` `// Driver program to test above functions``int` `main()``{``    ``string s = ``"geeksforgeeks"``;``    ``int` `Q = 4;``    ``preProcess(s);` `    ``cout << findCharFreq(0, 5, ``'e'``) << endl;``    ``cout << findCharFreq(2, 6, ``'f'``) << endl;``    ``cout << findCharFreq(4, 7, ``'m'``) << endl;``    ``cout << findCharFreq(0, 12, ``'e'``) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find occurrence``// of character in substring l to r``import` `java.util.*;` `class` `GFG``{` `static` `int` `MAX_LEN = ``1005``;``static` `int` `MAX_CHAR = ``26``;` `// To store count of all character``static` `int` `[][]cnt = ``new` `int``[MAX_LEN][MAX_CHAR];` `// To pre-process string from``// 0 to size of string``static` `void` `preProcess(String s)``{``    ``int` `n = s.length();` `    ``// Store occurrence of``    ``// character i``    ``for` `(``int` `i = ``0``; i < n; i++)``        ``cnt[i][s.charAt(i) - ``'a'``]++;` `    ``// Store occurrence o``    ``// all character upto i``    ``for` `(``int` `i = ``1``; i < n; i++)``    ``{``        ``for` `(``int` `j = ``0``; j < ``26``; j++)``            ``cnt[i][j] += cnt[i - ``1``][j];``    ``}``}` `// To return occurrence of``// character in range l to r``static` `int` `findCharFreq(``int` `l, ``int` `r, ``char` `c)``{``    ``// Return occurrence of character``    ``// from 0 to r minus its``    ``// occurrence from 0 to l``    ``return` `(cnt[r][(c) - ``97``] - cnt[l][(c) - ``97``]);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``String s = ``"geeksforgeeks"``;``    ``int` `Q = ``4``;``    ``preProcess(s);` `    ``System.out.println(findCharFreq(``0``, ``5``, ``'e'``));``    ``System.out.println(findCharFreq(``2``, ``6``, ``'f'``));``    ``System.out.println(findCharFreq(``4``, ``7``, ``'m'``));``    ``System.out.println(findCharFreq(``0``, ``12``, ``'e'``));``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 program to find occurrence``# of character in substring l to r``MAX_LEN, MAX_CHAR ``=` `1005``, ``26` `# To store count of all character``cnt ``=` `[[``0` `for` `i ``in` `range``(MAX_CHAR)]``          ``for` `j ``in` `range``(MAX_LEN)]` `# To pre-process string from``# 0 to size of string``def` `preProcess(s):` `    ``n ``=` `len``(s)` `    ``# Store occurrence of character i``    ``for` `i ``in` `range``(``0``, n):``        ``cnt[i][``ord``(s[i]) ``-` `ord``(``'a'``)] ``+``=` `1` `    ``# Store occurrence o``    ``# all character upto i``    ``for` `i ``in` `range``(``1``, n):``        ``for` `j ``in` `range``(``0``, ``26``):``            ``cnt[i][j] ``+``=` `cnt[i ``-` `1``][j]``    ` `# To return occurrence of``# character in range l to r``def` `findCharFreq(l, r, c):` `    ``# Return occurrence of character``    ``# from 0 to r minus its``    ``# occurrence from 0 to l``    ``return` `(cnt[r][``ord``(c) ``-` `97``] ``-``            ``cnt[l ``-` `1``][``ord``(c) ``-` `97``])` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``s ``=` `"geeksforgeeks"``    ``Q ``=` `4``    ``preProcess(s)` `    ``print``(findCharFreq(``0``, ``5``, ``'e'``))``    ``print``(findCharFreq(``2``, ``6``, ``'f'``))``    ``print``(findCharFreq(``4``, ``7``, ``'m'``))``    ``print``(findCharFreq(``0``, ``12``, ``'e'``))` `# This code is contributed by Rituraj Jain``# and Edited by Md Azharuddin`

## C#

 `// C# program to find occurrence``// of character in substring l to r``using` `System;` `class` `GFG``{``    ``static` `int` `MAX_LEN = 1005;``    ``static` `int` `MAX_CHAR = 26;` `    ``// To store count of all character``    ``static` `int``[,] cnt = ``new` `int``[MAX_LEN,``                                ``MAX_CHAR];` `    ``// To pre-process string from``    ``// 0 to size of string``    ``static` `void` `preProcess(``string` `s)``    ``{``        ``int` `n = s.Length;` `        ``// Store occurrence of``        ``// character i``        ``for` `(``int` `i = 0; i < n; i++)``            ``cnt[i, s[i] - ``'a'``]++;` `        ``// Store occurrence o``        ``// all character upto i``        ``for` `(``int` `i = 1; i < n; i++)``        ``{``            ``for` `(``int` `j = 0; j < 26; j++)``                ``cnt[i, j] += cnt[i - 1, j];``        ``}``    ``}` `    ``// To return occurrence of``    ``// character in range l to r``    ``static` `int` `findCharFreq(``int` `l, ``int` `r, ``char` `c)``    ``{``        ``// Return occurrence of character``        ``// from 0 to r minus its``        ``// occurrence from 0 to l``        ``return` `(cnt[r, c - 97] - cnt[l, c - 97]);``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``string` `s = ``"geeksforgeeks"``;``        ``int` `Q = 4;``        ``preProcess(s);` `        ``Console.WriteLine(findCharFreq(0, 5, ``'e'``));``        ``Console.WriteLine(findCharFreq(2, 6, ``'f'``));``        ``Console.WriteLine(findCharFreq(4, 7, ``'m'``));``        ``Console.WriteLine(findCharFreq(0, 12, ``'e'``));``    ``}``}` `// This code is contributed by``// sanjeev2552`

Output:

```2
1
0
4```

My Personal Notes arrow_drop_up