Count Vowel Substrings of a String

Easy

Topics
Hash TableStringSliding Window

A vowel substring is a contiguous, non-empty piece of s that consists only of vowels and contains all five of a, e, i, o and u.

Return the number of vowel substrings of s. Two pieces at different positions count separately even if they read the same.

Example 1

a0e1i2o3u4u5
vowel
A qualifying piece must be all vowels and must contain all five of a, e, i, o, u, so a consonant is a wall no piece can cross. Exactly 2 pieces of this string qualify. Repeats are allowed, and two identical pieces at different positions count twice.
One cell per character; the small numbers underneath are indices.
Input:  s = "aeiouu"
Output: 2
Explanation: The pieces "aeiou" and "aeiouu" both qualify.

Example 2

u0n1i2c3o4r5n6a7r8i9h10a11n12
vowelconsonant, a hard boundary
No run of vowels here holds all five, so the answer is 0.
Input:  s = "unicornarihan"
Output: 0
Explanation: Every run of vowels here is broken by a consonant before all five can appear.

Example 3

c0u1a2i3e4u5o6u7a8c9
vowelconsonant, a hard boundary
Input:  s = "cuaieuouac"
Output: 7
Explanation: All seven lie inside the run "uaieuoua"; the consonants at each end can never be part of one.

Constraints

  • 1 <= s.length <= 100
  • s consists of lowercase English letters only
Editorial — intuition, approach, complexity and pitfalls

Intuition

The two conditions pull in opposite directions and that is what makes the problem easy once seen. "Only vowels" means a qualifying piece can never cross a consonant, so the string breaks into independent runs of vowels and the answer is the sum over those runs. "All five vowels" is then a question asked inside a single run.

Within a run, the piece ending at a given position has a longest valid start: push the start forward while all five vowels are still present, and every start at or before that point also works. So the count of valid pieces ending here is just the number of allowed starts, which a shrinking window gives directly.

At the input sizes here a straightforward double loop over starts and ends is also fine, and the editorial below takes it for clarity; the window version is what the problem is really probing for.

Approach

  1. Walk the string, splitting it at consonants into maximal runs of vowels.
  2. For each run, consider every start and extend the end, tracking which of the five vowels the piece currently holds.
  3. Count a piece as soon as all five are present, and keep counting as the end advances — adding more vowels never invalidates it.
  4. Sum the counts over all runs.

Walkthrough

s = "cuaieuouac"

consonants at index 0 and 9, so one run: "uaieuoua"
   indices within the run: u a i e u o u a
                           0 1 2 3 4 5 6 7

The only o is at 5, so every valid piece must contain index
5; the only i is at 2 and the only e at 3, so every valid
piece must start at or before 2.

 start 0: ends 5, 6, 7  ->  3 pieces
 start 1: ends 5, 6, 7  ->  3 pieces   (u at 4 supplies the u)
 start 2: end 7 only    ->  1 piece    (needs the a at 7)
 start 3+: no i left    ->  0

3 + 3 + 1 = 7

Complexity

  • Time — O(n^2): every start extends until it meets a consonant. The sliding-window version is O(n), which matters at larger limits than this problem sets.
  • Space — O(1): the set of seen vowels holds at most five entries.

Common Pitfalls

  • Counting pieces that contain all five vowels but also a consonant. The piece must be vowels and nothing else.
  • Breaking out of the inner loop once all five vowels are found. Extending further still gives valid pieces — "aeiouu" counts as well as "aeiou".
  • Requiring each vowel exactly once. Repeats are allowed, which is the whole of example 1.
  • Counting distinct substrings rather than positions. Two identical pieces at different offsets are two answers.

Full Solution — all languages

class Solution:
    def countVowelSubstrings(self, s):
        VOWELS = set("aeiou")
        total = 0
        for start in range(len(s)):
            if s[start] not in VOWELS:
                continue
            seen = set()
            for end in range(start, len(s)):
                if s[end] not in VOWELS:
                    break
                seen.add(s[end])
                if len(seen) == 5:
                    total += 1
        return total
SensAI
Beta
Tap the mic to stop voice mode