Count of K length substrings containing exactly X vowels
Last Updated :
11 Jan, 2022
Given string str of N characters containing both uppercase and lowercase English alphabets, the task is to find the count of substrings of size K containing exactly X vowels.
Examples:
Input: str = “GeeksForGeeks”, K = 2, X = 2
Output: 2
Explanation: The given string only contains 2 substrings of size 2 consisting of 2 vowels. They are “ee” and “ee”.
Input: str = “TrueGeek”, K = 3, X = 2
Output: 5
Approach: The given problem can be solved using a sliding window technique by maintaining a window of size K and keeping track of the number of vowels encountered in the current window. If the count of vowels in the current window is equal to X, increment the final required count by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 128
bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U' );
}
int cntSubstr(string str, int K, int X)
{
int vow = 0;
for ( int i = 0; i < K; i++)
if (isVowel(str[i]))
vow++;
int ans = vow == X ? 1 : 0;
for ( int i = 1; i < str.length(); i++) {
vow = isVowel(str[i - 1]) ? vow - 1 : vow;
vow = isVowel(str[i - 1 + K]) ? vow + 1 : vow;
if (vow == X)
ans++;
}
return ans;
}
int main( void )
{
string s = "TrueGeek" ;
int K = 3, X = 2;
cout << cntSubstr(s, K, X);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U' );
}
static int cntSubstr(String str, int K, int X)
{
int vow = 0 ;
for ( int i = 0 ; i < K; i++)
if (isVowel(str.charAt(i)))
vow++;
int ans = vow == X ? 1 : 0 ;
for ( int i = 1 ; i < str.length(); i++) {
vow = isVowel(str.charAt(i - 1 )) ? vow - 1 : vow;
if (i - 1 + K < str.length())
vow = isVowel(str.charAt(i - 1 + K)) ? vow + 1 : vow;
if (vow == X)
ans++;
}
return ans;
}
public static void main (String[] args) {
String s = "TrueGeek" ;
int K = 3 , X = 2 ;
System.out.println(cntSubstr(s, K, X));
}
}
|
Python3
MAX = 128
def isVowel(x):
return (x = = 'a' or x = = 'e' or x = = 'i' or
x = = 'o' or x = = 'u' or x = = 'A' or
x = = 'E' or x = = 'I' or x = = 'O' or
x = = 'U' )
def cntSubstr( str , K, X):
vow = 0
for i in range ( 0 , K):
if (isVowel( str [i])):
vow + = 1
ans = 1 if vow = = X else 0
for i in range ( 1 , len ( str ) - K + 1 ):
vow = vow - 1 if isVowel( str [i - 1 ]) else vow
vow = vow + 1 if isVowel( str [i - 1 + K]) else vow
if (vow = = X):
ans + = 1
return ans
if __name__ = = "__main__" :
s = "TrueGeek"
K, X = 3 , 2
print (cntSubstr(s, K, X))
|
C#
using System;
class GFG
{
static bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U' );
}
static int cntSubstr( string str, int K, int X)
{
int vow = 0;
for ( int i = 0; i < K; i++)
if (isVowel(str[i]))
vow++;
int ans = vow == X ? 1 : 0;
for ( int i = 1; i < str.Length; i++) {
vow = isVowel(str[i - 1]) ? vow - 1 : vow;
if (i - 1 + K < str.Length)
vow = isVowel(str[i - 1 + K]) ? vow + 1 : vow;
if (vow == X)
ans++;
}
return ans;
}
public static void Main()
{
string s = "TrueGeek" ;
int K = 3, X = 2;
Console.Write(cntSubstr(s, K, X));
}
}
|
Javascript
<script>
let MAX = 128
function isVowel(x) {
return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U' );
}
function cntSubstr(str, K, X)
{
let vow = 0;
for (let i = 0; i < K; i++)
if (isVowel(str[i]))
vow++;
let ans = vow == X ? 1 : 0;
for (let i = 1; i < str.length; i++) {
vow = isVowel(str[i - 1]) ? vow - 1 : vow;
vow = isVowel(str[i - 1 + K]) ? vow + 1 : vow;
if (vow == X)
ans++;
}
return ans;
}
let s = "TrueGeek" ;
let K = 3, X = 2;
document.write(cntSubstr(s, K, X));
</script>
|
Time Complexity: O(N)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...