Count of K length substring containing at most X distinct vowels
Given string str of size N containing both uppercase and lowercase letters, and two integers K and X. The task is to find the count of substrings of size K containing at most X distinct vowels.
Examples:
Input: str = “TrueGoik”, K = 3, X = 2
Output: 6
Explanation: The five such substrings are “Tru”, “rue”, “ueG”, “eGo”, “Goi” and”oik”.
Input: str = “GeeksForGeeks”, K = 2, X = 2
Output: 12
Explanation: “Ge”, “ee”, “ek”, “ks”, “sf”, “fo”, “or”, “rG”, “Ge”, “ee”, “ek” and “ks”.
Approach: To solve the problem, first one have to generate all the substrings of length K. Then in each substring check if number of distinct vowels is less than X or not. Follow the steps mentioned below.
- First generate all substrings of length K starting from each index i in [0, N – K].
- Then for each substring of length K, do the following:
- Keep a hash to store the occurrences of unique vowels.
- Check if a new character in the substring is a vowel or not.
- If it is a vowel, increment its occurrence in the hash and keep a count of distinct vowels found
- Now for each substring, if the distinct count of vowels is less than or equal to X, increment the final count.
- When all the substrings have been considered, print the final count.
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 getIndex( char ch)
{
return (ch - 'A' > 26 ? ch - 'a' :
ch - 'A' );
}
int get(string str, int k, int x)
{
int n = str.length();
int res = 0;
for ( int i = 0; i <= n - k; i++) {
int dist_count = 0;
vector< int > cnt(26, 0);
for ( int j = i; j < i + k; j++) {
if (isVowel(str[j])
&& cnt[getIndex(str[j])]
== 0) {
dist_count++;
}
cnt[getIndex(str[j])]++;
}
if (dist_count <= x)
res++;
}
return res;
}
int main( void )
{
string s = "TrueGoik" ;
int K = 3, X = 2;
cout << get(s, K, X);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int MAX = 128 ;
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 getIndex( char ch) {
return (ch - 'A' > 26 ? ch - 'a' : ch - 'A' );
}
static int get(String str, int k, int x) {
int n = str.length();
int res = 0 ;
for ( int i = 0 ; i <= n - k; i++) {
int dist_count = 0 ;
int [] cnt = new int [ 26 ];
Arrays.fill(cnt, 0 );
for ( int j = i; j < i + k; j++) {
if (isVowel(str.charAt(j))
&& cnt[getIndex(str.charAt(j))] == 0 ) {
dist_count++;
}
cnt[getIndex(str.charAt(j))]++;
}
if (dist_count <= x)
res++;
}
return res;
}
public static void main(String args[]) {
String s = "TrueGoik" ;
int K = 3 , X = 2 ;
System.out.println(get(s, K, X));
}
}
|
Python3
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 getIndex(ch):
return ( ord (ch) - ord ( 'a' )) if ( ord (ch) - ord ( 'A' )) > 26 else ( ord (ch) - ord ( 'A' ))
def get( str , k, x):
n = len ( str )
res = 0
for i in range (n - k + 1 ):
dist_count = 0
cnt = [ 0 ] * 26
for j in range (i, i + k):
if (isVowel( str [j]) and cnt[getIndex( str [j])] = = 0 ):
dist_count + = 1
cnt[getIndex( str [j])] + = 1
if (dist_count < = x):
res + = 1
return res
s = "TrueGoik"
K = 3
X = 2
print (get(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 getIndex( char ch)
{
return (ch - 'A' > 26 ? ch - 'a' : ch - 'A' );
}
static int get ( string str, int k, int x)
{
int n = str.Length;
int res = 0;
for ( int i = 0; i <= n - k; i++) {
int dist_count = 0;
int [] cnt = new int [26];
for ( int j = i; j < i + k; j++) {
if (isVowel(str[j])
&& cnt[getIndex(str[j])] == 0) {
dist_count++;
}
cnt[getIndex(str[j])]++;
}
if (dist_count <= x)
res++;
}
return res;
}
public static void Main()
{
string s = "TrueGoik" ;
int K = 3, X = 2;
Console.WriteLine( get (s, K, X));
}
}
|
Javascript
<script>
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 getIndex(ch) {
return ((ch.charCodeAt(0) - 'A' .charCodeAt(0)) > 26 ? (ch.charCodeAt(0) - 'a' .charCodeAt(0)) :
(ch.charCodeAt(0) - 'A' .charCodeAt(0)));
}
function get(str, k, x) {
let n = str.length;
let res = 0;
for (let i = 0; i <= n - k; i++) {
let dist_count = 0;
let cnt = new Array(26).fill(0);
for (let j = i; j < i + k; j++) {
if (isVowel(str[j])
&& cnt[getIndex(str[j])]
== 0) {
dist_count++;
}
cnt[getIndex(str[j])]++;
}
if (dist_count <= x)
res++;
}
return res;
}
let s = "TrueGoik" ;
let K = 3, X = 2;
document.write(get(s, K, X));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(N * K)
Last Updated :
12 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...