# Queries for frequencies of characters in substrings in C++

In this problem, we are given a string. And Q queries each has two integers l and r and character ch. our task is to create a program to solve the queries for frequencies of characters in substrings in C++.

Problem description: Here for each querry, we will find the frequency of occurrence of the character ‘ch’ in the substring str[l...r].

Let’s take an example to understand the problem,

## Input

str = “tutorialspoint”
Q = 2
0 6 t
5 13 i

## Output

2 2

## Explanation

For query 1 − the substring is “tutoria”, the character t appears 2 times.

For query 2 − the substring is “ialspoint”, the character i appears 2 times

## Solution approach

The easy and straight forward approach to solve the problem is by traversing he string from l to r for each querry and counting the occurrence of the haracter ch in the string.

Program to illustrate the working of our solution,

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

struct Query{
int l, r;
char ch;
};
int CalcCharFreq(string str, Query queries){
int count = 0;
for(int i = queries.l; i < queries.r; i++){
if(str[i] == queries.ch )
count++;
}
return count;
}

int main(){
string str = "tutorialspoint";
int Q = 2;
Query queries[Q];
queries.l = 0;
queries.r = 5;
queries.ch = 't';
queries.l = 5;
queries.r = 13;
queries.ch = 'i';
for(int i = 0; i<Q; i++)
cout<<"For Query "<<(i+1)<<": The frequency of occurrence of character '"<<queries[i].ch<<"' is "<<CalcCharFreq(str, queries[i])<<"\n";
return 0;
}

## Output

For Query 1: The frequency of occurrence of character 't' is 2
For Query 2: The frequency of occurrence of character 'i' is 2

Another approach to solving the problem is by using a pre-computed array. Here, we will create a 2D array that will store the frequency of characters upto that specific index i.e. freq will store the frequency of character ‘c’ at the index 2. Initially, all frequencies are 0.

Then, we will pre-compute the frequency of the character at every index value. After that, we will find the frequency of the character in the range by subtracting the frequency of occurrence at index l from that at index r.

Let’s take an example to understand the problem,

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int charFreq;

struct Query{
int l, r;
char ch;
};

void countCharFreq(string str, int size){

memset(charFreq, 0, sizeof(int));
for (int i = 0; i < size; i++){
charFreq[i][str[i] - 'a']++;
}
for (int i = 1; i < size; i++) {
for (int j = 0; j < 26; j++)
charFreq[i][j] += charFreq[i - 1][j] ;
}
}

int CalcCharFreq(Query queries){
return charFreq[queries.r][queries.ch - 'a'] - charFreq[queries.l][queries.ch - 'a'];
}

int main(){
string str = "tutorialspoint";
int size = str.length();
int Q = 2;
countCharFreq(str, size);
Query queries[Q];
queries.l = 1;
queries.r = 13;
queries.ch = 't';
queries.l = 4;
queries.r = 13;
queries.ch = 'i';
for(int i = 0; i<Q; i++)
cout<<"For Query "<<(i+1)<<": The frequency of occurrence of character '"<<queries[i].ch<<"' is "   <<CalcCharFreq( queries[i])<<"\n";
return 0;
}

## Output

For Query 1: The frequency of occurrence of character 't' is 2
For Query 2: The frequency of occurrence of character 'i' is 2