Count Vowel Substrings of a String - Problem

Imagine you're a linguist studying complete vowel patterns in words! Your task is to find all the special substrings that contain every single vowel in the English language.

A vowel substring is a contiguous sequence of characters that:

  • Contains only vowels ('a', 'e', 'i', 'o', 'u')
  • Has all five vowels present at least once

Given a string word, return the count of such magical vowel substrings.

Example: In "aeiouu", the substring "aeiou" qualifies because it contains only vowels and has all five different vowels!

Input & Output

example_1.py โ€” Basic Case
$ Input: word = "aeiouu"
โ€บ Output: 2
๐Ÿ’ก Note: The vowel substrings are "aeiou" and "aeiouu". Both contain only vowels and have all five vowels present.
example_2.py โ€” With Consonants
$ Input: word = "unicornarihan"
โ€บ Output: 0
๐Ÿ’ก Note: Although there are vowels, no substring contains only vowels AND has all five vowels present. The consonants break up the vowel sequences.
example_3.py โ€” Complex Pattern
$ Input: word = "cuaeiouueioa"
โ€บ Output: 7
๐Ÿ’ก Note: Multiple valid substrings exist within the vowel-only section "aeiouueioa". Each must contain all five vowels: aeiou, aeiouu, aeiouue, aeiouuei, aeiouueio, aeiouueioa, uueioa.

Visualization

Tap to expand
๐Ÿ” The Vowel Substring DetectiveEvidence Chain: "cuaeiouu"cSKIPuSKIPaSTARTeiouCASE!u๐Ÿ•ต๏ธ Detective's Evidence Collection:Step 1: Found 'a' โ†’ Evidence: {a}Step 2: Found 'e' โ†’ Evidence: {a,e}Step 3: Found 'i' โ†’ Evidence: {a,e,i}Step 4: Found 'o' โ†’ Evidence: {a,e,i,o}Step 5: Found 'u' โ†’ Complete Case! {a,e,i,o,u}Step 6: Found 'u' โ†’ Another Case! "aeiouu"๐ŸŽฏ Cases Solved:"aeiou" โœ“"aeiouu" โœ“Total: 2 Complete Cases๐Ÿ’ก Detective's MethodSkip irrelevant clues (consonants)Count complete evidence chains efficiently
Understanding the Visualization
1
Skip the Noise
Like a detective ignoring irrelevant clues, skip any consonants since they can't be part of our target
2
Start Collecting Evidence
When you find a vowel, start your evidence collection - track each unique vowel type you encounter
3
Build Complete Cases
Once you have all 5 evidence types, you've solved one case! Every extension still gives you another complete case
4
Break on Contamination
If you hit a consonant, your evidence chain is broken - start fresh from the next vowel
Key Takeaway
๐ŸŽฏ Key Insight: The detective approach (optimized sliding window) is like having a smart evidence collection strategy - skip the noise, track unique evidence types, and count complete cases as you build them. This gives us O(n) efficiency instead of checking every possible evidence combination!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through string with constant work per character

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using fixed-size hash map for 5 vowels plus a few variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค word.length โ‰ค 100
  • word consists of lowercase English letters only
  • All five vowels (a, e, i, o, u) must be present in a valid substring
  • Valid substrings must contain only vowel characters
Asked in
Amazon 23 Google 18 Microsoft 15 Meta 12
28.9K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen