Queries to find the count of vowels in the substrings of the given string
Last Updated :
29 Dec, 2022
Given string str of length N and Q queries where every query consists of two integers L and R. For every query, the task is to find the count of vowels in the substring str[L…R].
Examples:
Input: str = “geeksforgeeks”, q[][] = {{1, 3}, {2, 4}, {1, 9}}
Output:
2
1
4
Query 1: “eek” has 2 vowels.
Query 2: “eks” has 1 vowel.
Query 3: “eeksforge” has 2 vowels.
Input: str = “aaaa”, q[][] = {{1, 3}, {1, 4}}
Output:
3
3
Naive approach: For every query, traverse the string from the Lth character to the Rth character and find the count of vowels.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 2
bool isVowel( char ch)
{
return (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' );
}
int countVowels(string str, int l, int r)
{
int cnt = 0;
for ( int i = l; i <= r; i++) {
if (isVowel(str[i]))
cnt++;
}
return cnt;
}
void performQueries(string str, int queries[][N], int q)
{
for ( int i = 0; i < q; i++) {
cout << countVowels(str, queries[i][0],
queries[i][1]) << "\n" ;
}
}
int main()
{
string str = "geeksforgeeks" ;
int queries[][N] = { { 1, 3 }, { 2, 4 }, { 1, 9 } };
int q = ( sizeof (queries)
/ sizeof (queries[0]));
performQueries(str, queries, q);
return 0;
}
|
Java
class GFG
{
static int N = 2 ;
static boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static int countVowels(String str,
int l, int r)
{
int cnt = 0 ;
for ( int i = l; i <= r; i++)
{
if (isVowel(str.charAt(i)))
cnt++;
}
return cnt;
}
static void performQueries(String str,
int queries[][],
int q)
{
for ( int i = 0 ; i < q; i++)
{
System.out.println(countVowels(str, queries[i][ 0 ],
queries[i][ 1 ]));
}
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int queries[][] = { { 1 , 3 }, { 2 , 4 },
{ 1 , 9 } };
int q = queries.length;
performQueries(str, queries, q);
}
}
|
Python3
N = 2 ;
def isVowel(ch) :
return (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' );
def countVowels(string, l, r) :
cnt = 0 ;
for i in range (l, r + 1 ) :
if (isVowel(string[i])) :
cnt + = 1 ;
return cnt;
def performQueries(string, queries, q) :
for i in range (q) :
print (countVowels(string, queries[i][ 0 ],
queries[i][ 1 ]));
if __name__ = = "__main__" :
string = "geeksforgeeks" ;
queries = [ [ 1 , 3 ],
[ 2 , 4 ],
[ 1 , 9 ] ];
q = len (queries)
performQueries(string, queries, q);
|
C#
using System;
class GFG
{
static int N = 2;
static Boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static int countVowels(String str,
int l, int r)
{
int cnt = 0;
for ( int i = l; i <= r; i++)
{
if (isVowel(str[i]))
cnt++;
}
return cnt;
}
static void performQueries(String str,
int [,]queries,
int q)
{
for ( int i = 0; i < q; i++)
{
Console.WriteLine(countVowels(str, queries[i, 0],
queries[i, 1]));
}
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int [,]queries = { { 1, 3 }, { 2, 4 },
{ 1, 9 } };
int q = queries.GetLength(0);
performQueries(str, queries, q);
}
}
|
Javascript
<script>
let N = 2;
function isVowel(ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
function countVowels(str, l, r)
{
let cnt = 0;
for (let i = l; i <= r; i++)
{
if (isVowel(str[i]))
cnt++;
}
return cnt;
}
function performQueries(str, queries, q)
{
for (let i = 0; i < q; i++)
{
document.write(countVowels(str, queries[i][0],
queries[i][1]) + "</br>" );
}
}
let str = "geeksforgeeks" ;
let queries = [ [ 1, 3 ], [ 2, 4 ], [ 1, 9 ] ];
let q = queries.length;
performQueries(str, queries, q);
</script>
|
Time Complexity: O(N * Q) where N is the length of a string and Q is the number of queries.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient approach: Create a prefix array pre[] where pre[i] will store the count vowels in the substring str[0…i]. Now, the count of vowels in the range [L, R] can be easily calculated in O(1) as pre[R] – pre[L – 1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 2
bool isVowel( char ch)
{
return (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' );
}
void performQueries(string str, int len,
int queries[][N], int q)
{
int pre[len];
if (isVowel(str[0]))
pre[0] = 1;
else
pre[0] = 0;
for ( int i = 1; i < len; i++) {
if (isVowel(str[i]))
pre[i] = 1 + pre[i - 1];
else
pre[i] = pre[i - 1];
}
for ( int i = 0; i < q; i++) {
if (queries[i][0] == 0) {
cout << pre[queries[i][1]] << "\n" ;
}
else {
cout << (pre[queries[i][1]]
- pre[queries[i][0] - 1])
<< "\n" ;
}
}
}
int main()
{
string str = "geeksforgeeks" ;
int len = str.length();
int queries[][N] = { { 1, 3 }, { 2, 4 }, { 1, 9 } };
int q = ( sizeof (queries)
/ sizeof (queries[0]));
performQueries(str, len, queries, q);
return 0;
}
|
Java
class GFG
{
static final int N = 2 ;
static Boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static void performQueries(String str, int len,
int queries[][], int q)
{
int []pre = new int [len];
if (isVowel(str.charAt( 0 )))
pre[ 0 ] = 1 ;
else
pre[ 0 ] = 0 ;
for ( int i = 1 ; i < len; i++)
{
if (isVowel(str.charAt(i)))
pre[i] = 1 + pre[i - 1 ];
else
pre[i] = pre[i - 1 ];
}
for ( int i = 0 ; i < q; i++)
{
if (queries[i][ 0 ] == 0 )
{
System.out.println(pre[queries[i][ 1 ]]);
}
else
{
System.out.println((pre[queries[i][ 1 ]] -
pre[queries[i][ 0 ] - 1 ]));
}
}
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int len = str.length();
int queries[][] = { { 1 , 3 },
{ 2 , 4 }, { 1 , 9 } };
int q = queries.length;
performQueries(str, len, queries, q);
}
}
|
Python 3
N = 2
def isVowel(ch):
return (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' )
def performQueries(str1, len1, queries, q):
pre = [ 0 for i in range (len1)]
if (isVowel(str1[ 0 ])):
pre[ 0 ] = 1
else :
pre[ 0 ] = 0
for i in range ( 0 , len1, 1 ):
if (isVowel(str1[i])):
pre[i] = 1 + pre[i - 1 ]
else :
pre[i] = pre[i - 1 ]
for i in range (q):
if (queries[i][ 0 ] = = 0 ):
print (pre[queries[i][ 1 ]])
else :
print (pre[queries[i][ 1 ]] -
pre[queries[i][ 0 ] - 1 ])
if __name__ = = '__main__' :
str1 = "geeksforgeeks"
len1 = len (str1)
queries = [[ 1 , 3 ], [ 2 , 4 ], [ 1 , 9 ]]
q = len (queries)
performQueries(str1, len1, queries, q)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int N = 2;
static Boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static void performQueries(String str, int len,
int [,]queries, int q)
{
int []pre = new int [len];
if (isVowel(str[0]))
pre[0] = 1;
else
pre[0] = 0;
for ( int i = 1; i < len; i++)
{
if (isVowel(str[i]))
pre[i] = 1 + pre[i - 1];
else
pre[i] = pre[i - 1];
}
for ( int i = 0; i < q; i++)
{
if (queries[i, 0] == 0)
{
Console.WriteLine(pre[queries[i, 1]]);
}
else
{
Console.WriteLine((pre[queries[i, 1]] -
pre[queries[i, 0] - 1]));
}
}
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int len = str.Length;
int [,]queries = { { 1, 3 },
{ 2, 4 }, { 1, 9 } };
int q = queries.GetLength(0);
performQueries(str, len, queries, q);
}
}
|
Javascript
<script>
var N = 2;
function isVowel(ch)
{
return (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' );
}
function performQueries(str, len, queries, q)
{
var pre = Array(len);
if (isVowel(str[0]))
pre[0] = 1;
else
pre[0] = 0;
for ( var i = 1; i < len; i++) {
if (isVowel(str[i]))
pre[i] = 1 + pre[i - 1];
else
pre[i] = pre[i - 1];
}
for ( var i = 0; i < q; i++) {
if (queries[i][0] == 0) {
document.write( pre[queries[i][1]] + "<br>" );
}
else {
document.write(pre[queries[i][1]]
- pre[queries[i][0] - 1]
+ "<br>" );
}
}
}
var str = "geeksforgeeks" ;
var len = str.length;
var queries = [ [ 1, 3 ], [ 2, 4 ], [ 1, 9 ] ];
var q = queries.length
performQueries(str, len, queries, q);
</script>
|
Time Complexity: O(N) for pre-computation and O(1) for every query.
Auxiliary Space: O(N), where n is the length of the given string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...