Count of substrings containing exactly K distinct vowels
Given string str of size N containing both uppercase and lowercase letters, and an integer K. The task is to find the count of substrings containing exactly K distinct vowels.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: str = “aeiou”, K = 2
Output: 4
Explanation: The substrings having two distinct vowels are “ae”, “ei”, “io” and “ou”.Input: str = “TrueGoik”, K = 3
Output: 5
Explanation: The substrings are “TrueGo”, “rueGo”, “ueGo”, “eGoi” and “eGoik”.
Approach: The problem can be solved by generating all the substrings. From the generated substrings count the ones having K distinct vowels. Follow the steps mentioned below to implement the approach:
- First generate all substrings starting from each index i in range [0, N]
- Then for each substring, follow the steps:
- Keep a hash array 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 K, increment the final count.
- If for any loop to find substrings starting from i, the count of distinct vowels exceeds K, or, the substring length has reached string length, break the loop and look for substrings starting from i+1.
- When all the substrings have been considered, print the final count.
Below is implementation of the above approach.
C++
// C++ program to count number of substrings// with exactly k distinct vowels#include <bits/stdc++.h>using namespace std; #define MAX 128 // Function to check whether// a character is vowel or notbool 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 count number of substrings// with exactly k unique vowelsint countkDist(string str, int k){ int n = str.length(); // Initialize result int res = 0; // Consider all substrings // beginning with str[i] for (int i = 0; i < n; 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 < n; 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 distinct vowels count // becomes k then increment result if (dist_count == k) res++; if (dist_count > k) break; } } return res;} // Driver codeint main(){ string str = "TrueGoik"; int K = 3; cout << countkDist(str, K) << endl; return 0;} |
5
Time Complexity: O(N2)
Auxiliary Space: O(N2)

