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 1
Explanation:
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 1
Explanation:
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:
C++
#include<bits/stdc++.h>
using namespace std;
void noOfChars(string s, char queries[][3], int q)
{
int n = s.length();
int dp[n + 1][26];
memset (dp, 0, sizeof (dp));
for ( int i = 0; i < n; i++)
{
dp[i + 1][s[i]- 'a' ]++;
for ( int j = 0; j < 26; j++)
{
dp[i + 1][j] += dp[i][j];
}
}
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' ;
cout << dp[r] - dp[l - 1] << " " ;
}
}
int main()
{
string S = "aabv" ;
char queries[2][3] = {{ 1, 2, 'a' },{ 2, 3, 'b' }};
noOfChars(S, queries, 2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void noOfChars(String s,
char [][] queries)
{
int n = s.length();
int dp[][] = new int [n + 1 ][ 26 ];
for ( int i = 0 ; i < n; i++) {
dp[i + 1 ][s.charAt(i) - 'a' ]++;
for ( int j = 0 ; j < 26 ; j++) {
dp[i + 1 ][j] += dp[i][j];
}
}
int q = queries.length;
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' ;
System.out.print(
dp[r] - dp[l - 1 ]
+ " " );
}
}
public static void main(String[] args)
{
String S = "aabv" ;
char queries[][]
= new char [][] { { 1 , 2 , 'a' },
{ 2 , 3 , 'b' } };
noOfChars(S, queries);
}
}
|
Python3
def noOfChars(s, queries):
n = len (s)
dp = [[ 0 for i in range ( 26 )]
for i in range (n + 1 )]
for i in range (n):
dp[i + 1 ][ ord (s[i]) - ord ( 'a' )] + = 1
for j in range ( 26 ):
dp[i + 1 ][j] + = dp[i][j]
q = len (queries)
for i in range (q):
l = queries[i][ 0 ]
r = queries[i][ 1 ]
c = ord (queries[i][ 2 ]) - ord ( 'a' )
print (dp[r] - dp[l - 1 ], end = " " )
if __name__ = = '__main__' :
S = "aabv"
queries = [ [ 1 , 2 , 'a' ],
[ 2 , 3 , 'b' ] ]
noOfChars(S, queries)
|
C#
using System;
public class GFG {
static void noOfChars(String s,
char [,] queries)
{
int n = s.Length;
int [,]dp = new int [n + 1, 26];
for ( int i = 0; i < n; i++) {
dp[i + 1, s[i] - 'a' ]++;
for ( int j = 0; j < 26; j++) {
dp[i + 1, j] += dp[i, j];
}
}
int q = queries.GetLength(0);
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' ;
Console.Write(
dp[r, c] - dp[l - 1, c]
+ " " );
}
}
public static void Main(String[] args)
{
String S = "aabv" ;
char [,]queries
= new char [,] { { ( char )1, ( char )2, 'a' },
{ ( char )2, ( char )3, 'b' } };
noOfChars(S, queries);
}
}
|
Javascript
<script>
function noOfChars(s,queries)
{
var n = s.length;
var dp =
Array(n+1).fill(0).map(x => Array(26).fill(0));
for ( var i = 0; i < n; i++) {
dp[i + 1][s.charAt(i).charCodeAt(0) -
'a' .charCodeAt(0)]++;
for ( var j = 0; j < 26; j++) {
dp[i + 1][j] += dp[i][j];
}
}
var q = queries.length;
for ( var i = 0; i < q; i++) {
var l =
String.fromCharCode(queries[i][0]).charCodeAt(0);
var r =
String.fromCharCode(queries[i][1]).charCodeAt(0);
var c =
queries[i][2].charCodeAt(0) - 'a' .charCodeAt(0);
document.write(
dp[r] - dp[l - 1]
+ " " );
}
}
var S = "aabv" ;
var queries
= [ [ 1, 2, 'a' ],
[ 2, 3, 'b' ] ];
noOfChars(S, queries);
</script>
|
Time Complexity: O(Q+N*26)
Auxiliary Space: O(N)