Count substrings that contain all vowels | SET 2

Given a string str containing lowercase alphabets, the task is to count the sub-strings that contain all the vowels at-least one time and there are no consonants (non-vowel characters) present in the sub-strings.

Examples:

Input: str = “aeoibsddaaeiouudb”
Output: 4
“aaeiouu”, “aeiouu”, “aeiou” and “aaeiou”

Input: str = “aeoisbddiouuaedf”
Output: 1

Input: str = “aeouisddaaeeiouua”
Output: 9

Approach: The idea is to extract all the maximum length sub-strings that contain only vowels. Now for all these sub-strings separately, we need to find the count of sub-strings that contains all the vowels at least once. This can be done using two-pointer technique.

Illustration of how to use the two-pointer technique in this case:

If string = “aeoibsddaaeiouudb”
The first step is to extract all maximum length sub-strings that contain only vowels which are:

  1. aeoi
  2. aaeiouu

Now, take the first string “aeoi”, it will not be counted because vowel ‘u’ is missing.
Then, take the second substring i.e. “aaeiouu”
Length of the string, n = 7
start = 0
index = 0
count = 0
We will run a loop till all the vowels are present at least once, so we stop at index 5 and start = 0.
Now our string is “aaeiou” and there are n – i substrings that contain vowels at least once and have string “aaeiou” as their prefix.
These substrings are: “aaeiou”, “aaeiouu”
count = count + (n – i) = 7 – 5 = 2
Now, count = 2

Then, increment start with 1. If substring between [start, index] i.e (1, 5) still contains vowels at least once then add (n – i).
These substrings are: “aeiou”, “aeiouu”
count = count + (n – i) = 7 – 5 = 2
Now, count = 2

Then start = 2, now substring becomes “eiouu”. Then no further count can be added because vowel ‘a’ is missing.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if c is a vowel
bool isVowel(char c)
{
    return (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u');
}
  
// Function to return the count of sub-strings
// that contain every vowel at least
// once and no consonant
int countSubstringsUtil(string s)
{
    int count = 0;
  
    // Map is used to store count of each vowel
    map<char, int> mp;
  
    int n = s.length();
  
    // Start index is set to 0 initially
    int start = 0;
  
    for (int i = 0; i < n; i++) {
        mp[s[i]]++;
  
        // If substring till now have all vowels
        // atleast once increment start index until
        // there are all vowels present between
        // (start, i) and add n - i each time
        while (mp['a'] > 0 && mp['e'] > 0
               && mp['i'] > 0 && mp['o'] > 0
               && mp['u'] > 0) {
            count += n - i;
            mp[s[start]]--;
            start++;
        }
    }
  
    return count;
}
  
// Function to extract all maximum length
// sub-strings in s that contain only vowels
// and then calls the countSubstringsUtil() to find
// the count of valid sub-strings in that string
int countSubstrings(string s)
{
    int count = 0;
    string temp = "";
  
    for (int i = 0; i < s.length(); i++) {
  
        // If current character is a vowel then
        // append it to the temp string
        if (isVowel(s[i])) {
            temp += s[i];
        }
  
        // The sub-string containing all vowels ends here
        else {
  
            // If there was a valid sub-string
            if (temp.length() > 0)
                count += countSubstringsUtil(temp);
  
            // Reset temp string
            temp = "";
        }
    }
  
    // For the last valid sub-string
    if (temp.length() > 0)
        count += countSubstringsUtil(temp);
  
    return count;
}
  
// Driver code
int main()
{
    string s = "aeouisddaaeeiouua";
  
    cout << countSubstrings(s) << endl;
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function that returns true if c is a vowel 
def isVowel(c) : 
  
    return (c == 'a' or c == 'e' or c == 'i'
            or c == 'o' or c == 'u'); 
  
  
# Function to return the count of sub-strings 
# that contain every vowel at least 
# once and no consonant 
def countSubstringsUtil(s) : 
  
    count = 0
  
    # Map is used to store count of each vowel 
    mp = dict.fromkeys(s,0); 
  
    n = len(s); 
  
    # Start index is set to 0 initially 
    start = 0
  
    for i in range(n) :
        mp[s[i]] += 1
  
        # If substring till now have all vowels 
        # atleast once increment start index until 
        # there are all vowels present between 
        # (start, i) and add n - i each time 
        while (mp['a'] > 0 and mp['e'] > 0
            and mp['i'] > 0 and mp['o'] > 0
            and mp['u'] > 0) :
            count += n - i; 
            mp[s[start]] -= 1
            start += 1
  
    return count; 
  
# Function to extract all maximum length 
# sub-strings in s that contain only vowels 
# and then calls the countSubstringsUtil() to find 
# the count of valid sub-strings in that string 
def countSubstrings(s) : 
  
    count = 0
    temp = ""; 
  
    for i in range(len(s)) :
  
        # If current character is a vowel then 
        # append it to the temp string 
        if (isVowel(s[i])) :
            temp += s[i]; 
  
        # The sub-string containing all vowels ends here 
        else :
  
            # If there was a valid sub-string 
            if (len(temp) > 0) :
                count += countSubstringsUtil(temp); 
  
            # Reset temp string 
            temp = ""; 
  
    # For the last valid sub-string 
    if (len(temp) > 0) :
        count += countSubstringsUtil(temp); 
  
    return count; 
  
# Driver code 
if __name__ == "__main__"
  
    s = "aeouisddaaeeiouua"
  
    print(countSubstrings(s)); 
  
# This code is contributed by AnkitRai01

chevron_right


Output:

9

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01