# Count of K length substring containing at most X distinct vowels

• Last Updated : 12 Jan, 2022

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++

 `// C++ code to implement above approach``#include ``using` `namespace` `std;` `#define MAX 128` `// Function to check whether``// a character is vowel or not``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'``);``}` `// Function to find the count of K length``// substring with at most x distinct vowels``int` `get(string str, ``int` `k, ``int` `x)``{``    ``int` `n = str.length();` `    ``// Initialize result``    ``int` `res = 0;` `    ``// Consider all substrings``    ``// beginning with str[i]``    ``for` `(``int` `i = 0; i <= n - k; i++) {``        ``int` `dist_count = 0;` `        ``// To store count of characters``        ``// from 'a' to 'z'``        ``vector<``int``> cnt(26, 0);` `        ``// Consider all substrings``        ``// between str[i..j]``        ``for` `(``int` `j = i; j < i + k; j++) {` `            ``// If this is a new vowels``            ``// for this substring,``            ``// increment dist_count.``            ``if` `(isVowel(str[j])``                ``&& cnt[getIndex(str[j])]``                ``== 0) {``                ``dist_count++;``            ``}` `            ``// Increment count of``            ``// current character``            ``cnt[getIndex(str[j])]++;``        ``}` `        ``// If count of distinct vowels``        ``// in current substring``        ``// of length k is less than``        ``// equal to x, then increment result.``        ``if` `(dist_count <= x)``            ``res++;``    ``}` `    ``return` `res;``}` `// Driver code``int` `main(``void``)``{``    ``string s = ``"TrueGoik"``;``    ``int` `K = 3, X = 2;``    ``cout << get(s, K, X);``    ``return` `0;``}`

## Java

 `// Java code to implement above approach``import` `java.util.Arrays;` `class` `GFG {``  ``static` `int` `MAX = ``128``;` `  ``// Function to check whether``  ``// a character is vowel or not``  ``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'``);``  ``}` `  ``// Function to find the count of K length``  ``// substring with at most x distinct vowels``  ``static` `int` `get(String str, ``int` `k, ``int` `x) {``    ``int` `n = str.length();` `    ``// Initialize result``    ``int` `res = ``0``;` `    ``// Consider all substrings``    ``// beginning with str[i]``    ``for` `(``int` `i = ``0``; i <= n - k; i++) {``      ``int` `dist_count = ``0``;` `      ``// To store count of characters``      ``// from 'a' to 'z'``      ``int``[] cnt = ``new` `int``[``26``];``      ``Arrays.fill(cnt, ``0``);` `      ``// Consider all substrings``      ``// between str[i..j]``      ``for` `(``int` `j = i; j < i + k; j++) {` `        ``// If this is a new vowels``        ``// for this substring,``        ``// increment dist_count.``        ``if` `(isVowel(str.charAt(j))``            ``&& cnt[getIndex(str.charAt(j))] == ``0``) {``          ``dist_count++;``        ``}` `        ``// Increment count of``        ``// current character``        ``cnt[getIndex(str.charAt(j))]++;``      ``}` `      ``// If count of distinct vowels``      ``// in current substring``      ``// of length k is less than``      ``// equal to x, then increment result.``      ``if` `(dist_count <= x)``        ``res++;``    ``}` `    ``return` `res;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String args[]) {``    ``String s = ``"TrueGoik"``;``    ``int` `K = ``3``, X = ``2``;``    ``System.out.println(get(s, K, X));``  ``}``}` `// This code is contributed by saurabh_jaiswal.`

## Python3

 `# Python code for the above approach` `# Function to check whether``# a character is vowel or not``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'``))` `# Function to find the count of K length``# substring with at most x distinct vowels``def` `get(``str``, k, x):``    ``n ``=` `len``(``str``)` `    ``# Initialize result``    ``res ``=` `0` `    ``# Consider all substrings``    ``# beginning with str[i]``    ``for` `i ``in` `range``(n ``-` `k ``+` `1``):``        ``dist_count ``=` `0` `        ``# To store count of characters``        ``# from 'a' to 'z'``        ``cnt ``=` `[``0``] ``*` `26` `        ``# Consider all substrings``        ``# between str[i..j]``        ``for` `j ``in` `range``(i, i ``+` `k):` `            ``# If this is a new vowels``            ``# for this substring,``            ``# increment dist_count.``            ``if` `(isVowel(``str``[j]) ``and` `cnt[getIndex(``str``[j])] ``=``=` `0``):``                ``dist_count ``+``=` `1` `            ``# Increment count of``            ``# current character``            ``cnt[getIndex(``str``[j])] ``+``=` `1` `        ``# If count of distinct vowels``        ``# in current substring``        ``# of length k is less than``        ``# equal to x, then increment result.``        ``if` `(dist_count <``=` `x):``            ``res ``+``=` `1` `    ``return` `res` `# Driver code` `s ``=` `"TrueGoik"``K ``=` `3``X ``=` `2``print``(get(s, K, X))` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# code to implement above approach``using` `System;``class` `GFG {` `  ``// Function to check whether``  ``// a character is vowel or not``  ``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'``);``  ``}` `  ``// Function to find the count of K length``  ``// substring with at most x distinct vowels``  ``static` `int` `get``(``string` `str, ``int` `k, ``int` `x)``  ``{``    ``int` `n = str.Length;` `    ``// Initialize result``    ``int` `res = 0;` `    ``// Consider all substrings``    ``// beginning with str[i]``    ``for` `(``int` `i = 0; i <= n - k; i++) {``      ``int` `dist_count = 0;` `      ``// To store count of characters``      ``// from 'a' to 'z'``      ``int``[] cnt = ``new` `int``;``      ``// Arrays.fill(cnt, 0);` `      ``// Consider all substrings``      ``// between str[i..j]``      ``for` `(``int` `j = i; j < i + k; j++) {` `        ``// If this is a new vowels``        ``// for this substring,``        ``// increment dist_count.``        ``if` `(isVowel(str[j])``            ``&& cnt[getIndex(str[j])] == 0) {``          ``dist_count++;``        ``}` `        ``// Increment count of``        ``// current character``        ``cnt[getIndex(str[j])]++;``      ``}` `      ``// If count of distinct vowels``      ``// in current substring``      ``// of length k is less than``      ``// equal to x, then increment result.``      ``if` `(dist_count <= x)``        ``res++;``    ``}` `    ``return` `res;``  ``}` `  ``// Driver code``  ``public` `static` `void` `Main()``  ``{``    ``string` `s = ``"TrueGoik"``;``    ``int` `K = 3, X = 2;``    ``Console.WriteLine(``get``(s, K, X));``  ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output
`6`

Time Complexity: O(N * K)
Auxiliary Space: O(N * K)

My Personal Notes arrow_drop_up