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[0].l = 0;
   queries[0].r = 5;
   queries[0].ch = 't';
   queries[1].l = 5;
   queries[1].r = 13;
   queries[1].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[3][2] 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[100][26];

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[0].l = 1;
   queries[0].r = 13;
   queries[0].ch = 't';
   queries[1].l = 4;
   queries[1].r = 13;
   queries[1].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

Updated on: 09-Sep-2020

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements