EasyHash TableStringUpdated Sep 2026

2062. Count Vowel Substrings of a String

Asked at Salesforce, Snowflake, DE Shaw

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Hash Table

Companies

3

Problem

You are given a string word. A vowel substring is a contiguous, non-empty piece of word that (1) contains only vowels — the letters a, e, i, o, u — and (2) contains all five of those vowels at least once.

Return the number of vowel substrings in word. Count each starting and ending position pair separately, even if the resulting text looks the same as another one.

Note that any substring containing a consonant is never valid, so you can stop scanning as soon as you hit one.

What counts as a vowel substring

worda e i o uaeiouu012345Indices 0 to 4 spell "aeiou" — all five vowels, sothis is valid.worda e i o u uaeiouu012345Extending to index 5 keeps all five vowels, so this isthe second valid substring.Answer for "aeiouu" = 2
Only stretches made of vowels count, and the stretch must contain all five vowels.

Constraints

  • ●1 <= word.length <= 100
  • ●word consists of lowercase English letters only.

Examples

Example 1
Inputword = "aeiouu"
Output2
WhyThe whole string is vowels only. The substring from index 0 to 4 ("aeiou") has all five vowels, and extending it by the last "u" still has all five. Those are the only two valid substrings.
Example 2
Inputword = "unicornarihan"
Output0
WhyThe string does contain all five vowels somewhere, but every substring that has all five also contains a consonant (like 'n', 'c' or 'r'), so none of them qualify.
Example 3
Inputword = "cuaieuouac"
Output7
WhyThe block "uaieuoua" between the two 'c's holds all five vowels. Starting at the 'u' (index 1), the first end that covers all five is index 5, giving 4 valid substrings. Starting at index 2, the first such end is index 7, giving 3 more. That is 4 + 3 = 7.

Asked At

3 companies

How to Think About It

1

Brute force: generate every possible substring and check if it contains all 5 vowels. For a string of length n, there are n*(n+1)/2 substrings. Checking each takes O(n). Total: O(n³). Works for small inputs.

2

Optimized brute force: for each starting index i, extend the ending index j and maintain a set of vowels seen. When the set has 5 vowels, all substrings starting at i and ending at j or beyond are valid (until the next non-vowel or end). This is O(n²) in the worst case.

3

Key observation: count all substrings that contain at least all 5 vowels = (count of substrings with at least all 5 vowels starting from each position). For each start i, find the first j where word[i:j+1] has all 5 vowels. Then n - j substrings starting at i are valid.

4

Sliding window approach: maintain a window with a Counter of vowels. Expand right. When the window has all 5 vowels, count how many valid substrings end at the current position. Shrink from left when possible to avoid overcounting.

5

Even simpler: count substrings with all vowels = total substrings with all vowels - substrings missing at least one vowel. Use inclusion-exclusion: for each vowel, count substrings that do NOT contain it. Subtract from total. But this is complex for 5 vowels.

6

Practical approach: use a nested loop with early termination. For each start i, walk forward until you have all 5 vowels. Count n - last_valid_end for that start. This is O(n * 5) = O(n) amortized since each character is visited once per start.

Optimal Approach

For each starting index i, find the smallest ending index j >= i such that word[i:j+1] contains all 5 vowels. If found, all substrings starting at i and ending at j, j+1, ..., n-1 are valid. Count n - j for that i.

Walkthrough with word = "aeiouu":

  • i=0: scan from 0. At j=4, we have "aeiou" (all 5 vowels). Count += 6 - 4 = 2. (substrings [0:4], [0:5])
  • i=1: scan from 1. At j=4, we have "eiou" (missing a). At j=5, we have "eiouu" (missing a). No valid end. Count += 0.
  • i=2: scan from 2. At j=5, we have "iouu" (missing a, e). No valid end. Count += 0.
  • i=3: scan from 3. At j=5, "ouu" (missing a, e, i). No valid end. Count += 0.
  • i=4: scan from 4. "uu" (missing a, e, i, o). No valid end. Count += 0.
  • i=5: "u" (missing most). No valid end. Count += 0.
  • Total: 2.

Walkthrough with word = "aabeiioou":

  • i=0: at j=8, all vowels appear. Count += 9 - 8 = 1.
  • Total: 1.

Time: O(n * 5) = O(n) amortized — each starting index scans forward, but the total scan is bounded by 5n since each vowel type only needs to be found once per window. Space: O(1) — fixed-size vowel set.

For each start, find the first end with all 5 vowels

wordseen = {a,e,i,o,u}aeiouu012345ijStart i=0: the first end with all five vowels is j=4,so add 6 - 4 = 2.wordseen = {e,i,o,u}aeiouu012345ijStart i=1: we reach the end with only 4 vowels seen,so nothing is added.for each i: scan j until 5 vowels or a consonantif 5 vowels: count += n - j
Once a start has all five vowels at some end j, every longer end is also valid, so we add n - j and move on.
ComplexityTime O(n)Space O(1)Each start index scans forward only until it has seen all five vowels or hits a consonant, and the inner scan is bounded by a constant number of vowel types, so the total work is linear in n.

What Trips People Up in Real Interviews

1

Confusing "substring" with "subsequence". Substrings are contiguous; subsequences are not. The problem asks for substrings, so you need contiguous character sequences.

2

Overcounting by not using the "first valid end" optimization. For each starting index, find the FIRST position where all 5 vowels appear. All substrings from that position to the end are valid. Do not iterate beyond the first valid end.

3

Forgetting that vowels can repeat. "aaeiou" still counts as having all 5 vowels. Your vowel set/counter must track presence, not count.

4

Not handling strings shorter than 5 characters. A string with fewer than 5 characters cannot contain all 5 vowels. Return 0 immediately.

5

Missing edge cases with consonants mixed in. "aebcdefgiou" has all vowels starting from index 0 at position 9. Substrings [0:9], [0:10], [0:11] are valid. Consonants between vowels do not break the substring.

Solution Code

O(n) time
def countVowelSubstrings(word):
    vowels = set('aeiou')
    n = len(word)
    count = 0
    for i in range(n):
        seen = set()
        for j in range(i, n):
            if word[j] in vowels:
                seen.add(word[j])
                if len(seen) == 5:
                    count += n - j
                    break
            else:
                break
    return count

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Count Vowel Substrings of a String problem?

Given a string `word`, return the number of substrings that contain every vowel (a, e, i, o, u) at least once. A substring is a contiguous sequence of characters. For example, "aeiou" has 1 vowel substring, and "aeiouu" has 2.

How do you solve Count Vowel Substrings of a String?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Count Vowel Substrings of a String?

Count Vowel Substrings of a String is asked at Salesforce, Snowflake, DE Shaw. It is a easy difficulty problem.

What are common mistakes on Count Vowel Substrings of a String?
  • Confusing "substring" with "subsequence". Substrings are contiguous; subsequences are not. The problem asks for substrings, so you need contiguous character sequences.
  • Overcounting by not using the "first valid end" optimization. For each starting index, find the FIRST position where all 5 vowels appear. All substrings from that position to the end are valid. Do not iterate beyond the first valid end.
  • Forgetting that vowels can repeat. "aaeiou" still counts as having all 5 vowels. Your vowel set/counter must track presence, not count.
  • Not handling strings shorter than 5 characters. A string with fewer than 5 characters cannot contain all 5 vowels. Return 0 immediately.
  • Missing edge cases with consonants mixed in. "aebcdefgiou" has all vowels starting from index 0 at position 9. Substrings [0:9], [0:10], [0:11] are valid. Consonants between vowels do not break the substring.