Count of K length substring containing at most X distinct vowels


In this problem, we need to find the total number of sub strings of length K containing at most X distinct vowels. We can solve the problem in two different ways. The first way is to get all sub strings and count distinct vowels in each sub string of length K. The second way is to use a map data structure and track the number of distinct vowels in the particular sub string.

Problem statement– We have given string str of length N. The string contains only alphabetical characters. Also, we have given K and X positive integers. We need to find a total number of different sub strings of length K containing the at most X distinct vowels.

Sample examples

Input– str = ‘point’, K = 3, X = 2

Output– 3

Explanation– The sub strings of length 3 containing at most 2 distinct vowels are −

  • The string ‘poi’ contains 2 vowels.

  • The string ‘oin’ contains two vowels.

  • The string ‘int’ contains 1 vowel.

Input– str = ‘sdfgh’, K = 3, X = 2

Output– 0

Explanation– The string doesn’t contain any vowel, so the output is zero.

Input– str = ‘aeiou’, K = 2, X = 2

Output– 4

Explanation– The sub strings of length 2 containing at most 2 distinct vowels are: ‘ae’, ‘ei’, ‘io’, and ‘ou’.

Approach 1

In this approach, we will get each sub string of length K from the given string. After that, we will check the number of distinct vowels in the sub string, and return the reuslt value based on the total number of vowels that the sub string contains.

Algorithm

  • Initialize the ‘cnt’ and ‘len’ variables.

  • Start traversing the string and traverse it from the 0th index to the len – k index.

  • Use the substr() method to get the sub string of length K starting from the index i.

  • Use the countDistVowels() function to count the distinct vowels in the sub string.

    • In the countDistVowels() function, use the map to store the presence of a particular vowel.

    • Access the values for the a, e, I, o, and u keys from the map and return their sum.

  • If the total number of the distinct vowel in the sub string is less than K, increment the ‘cnt’ value by 1.

  • When all iteration of the loop are completes, return the ‘cnt’ value.

Example

#include <bits/stdc++.h>
using namespace std;
int countDistVowels(string str){
   int dist = 0;
   int len = str.length();
   // map to store the presence of vowels
   unordered_map<char, int> mp;
   // insert vowels in the map
   for (int i = 0; i < len; i++){
      mp[str[i]] = 1;
   }
   // return the count of distinct vowels in a string
   return mp['a'] + mp['e'] + mp['i'] + mp['o'] + mp['u'];
}
int countSubStr(string str, int K, int X){
   // to store the total substring following the given condition
   int cnt = 0;
   int len = str.size();
   for (int i = 0; i <= len - K; i++){
      // get a substring of length K
      string s = str.substr(i, K);
      // If contDistVowels() function returns value less than X, increment cnt by 1.
      if (countDistVowels(s) <= X){
          cnt++;
      }
   }
   return cnt;
}
int main(void){
   string str = "point";
   int K = 3, X = 2;
   cout << "The number of a substring of length K containing at most X distinct vowels are " << countSubStr(str, K, X);
   return 0;
}

Output

The number of a substring of length K containing at most X distinct vowels are 3

Time complexity – O(N*K), as we count distinct vowels in each sub string

Space complexity – O(K), as we store the sub string of length K.

Approach 2

This approach uses the sliding window technique to solve the problem. We can count the total number of distinct vowels in the first window, and after that, we keep updating the distinct vowel count as we update the characters in the window.

Algorithm

  • Define the ‘vow’ variable and initialize with zero. Also, define the map to store vowel frequency

  • Convert the string into lowercase.

  • Count the total number of distinct vowels in the first sub string of length K starting from the index 0.

  • If the value of ‘vow’ is less than or equal to X, initialize the ‘cnt’ value by 1. Else with 0.

  • Traverse the string from 1st to the len – k index.

  • If (I – 1)th character is a vowel, decrease the value of ‘vow’ by 1 and update its frequency in the map.

  • We need to insert the (I – 1 + K)th character into the current window. If it is a vowel, and the frequency of it in the map is zero, increase the ‘vow’ by 1 and update the frequency in the map as it is a distinct vowel in the current window.

  • If the value of ‘vow’ is less than X, increment ‘cnt’ by 1.

  • Return the value of ‘cnt’ by 1.

Example

#include <bits/stdc++.h>
using namespace std;
// to check given character is vowel or not
bool isVowel(char ch) {
   return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
// function to count the number of substrings of length k containing at most k vowels
int countSubStr(string alpha, int K, int X) {
   // to store the count of vowels 
   int vow = 0;
   // creating an unordered map to store the count of vowels
   unordered_map<char, int> mp;
   // convert string to lowercase
   transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
   //  store total distinct vowels in the string of length k
   for (int i = 0; i < K; i++) {
      if (isVowel(alpha[i]) && mp[alpha[i]] == 0) {
          vow++;
          mp[alpha[i]]++;
      }
   }
   // If first substring contains at most x vowels, then increment cnt by 1
   int cnt = vow <= X ? 1 : 0;
   for (int i = 1; i <= alpha.length() -K; i++) {
      // remove i-1th character from the current window
      if (isVowel(alpha[i - 1])) {
         vow--;
         mp[alpha[i - 1]]--;
      }
      // Insert (i - 1 + K)th character
      if (isVowel(alpha[i - 1 + K]) && mp[alpha[i - 1 + K]] == 0) {
          vow++;
          mp[alpha[i - 1 + K]]++;
      }
      if (vow <= X)
         cnt++;
   }
   return cnt;
}
int main(void) {
   string str = "point";
   int K = 3, X = 2;
   cout << "The number of a substring of length K containing at most X distinct vowels are " << countSubStr(str, K, X);
   return 0;
}

Output

The number of a substring of length K containing at most X distinct vowels are 3

Time complexity – O(N) as we traverse the string.

Space complexity – O(1) as we use constant space.

We solved the problem using two approaches. The second approach uses the sliding window technique to optimize the code. Programmers can try to count the total number of sub strings of any length containing at most X distinct vowels and do more practice with such problems.

Updated on: 17-Aug-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements