Count Vowel Substrings of a String - Problem

A substring is a contiguous (non-empty) sequence of characters within a string.

A vowel substring is a substring that:

  • Only consists of vowels ('a', 'e', 'i', 'o', and 'u')
  • Has all five vowels present in it

Given a string word, return the number of vowel substrings in word.

Input & Output

Example 1 — Basic Case
$ Input: word = "aeiouu"
Output: 2
💡 Note: The vowel substrings with all 5 vowels are: "aeiou" (positions 0-4) and "aeiouu" (positions 0-5). Both contain all vowels a,e,i,o,u.
Example 2 — With Consonants
$ Input: word = "unicornarihan"
Output: 0
💡 Note: No substring contains all 5 vowels. The vowel-only segments are too short: "u", "i", "o", "a", "i", "a" - none have all 5 vowels.
Example 3 — Mixed Content
$ Input: word = "cuaieuouuc"
Output: 6
💡 Note: The vowel segment "uaieuouu" (positions 1-8) contains all 5 vowels. Valid substrings with all 5 vowels are: "uaieuo" (1-6), "uaieuou" (1-7), "uaieuouu" (1-8), "aieuo" (2-6), "aieuou" (2-7), "aieuouu" (2-8) - total of 6.

Constraints

  • 1 ≤ word.length ≤ 100
  • word consists of lowercase English letters only

Visualization

Tap to expand
Count Vowel Substrings of a String INPUT String: word a 0 e 1 i 2 o 3 u 4 u 5 Required Vowels: a e i o u Input: word = "aeiouu" Length: 6 characters All vowels only ALGORITHM STEPS 1 Find Vowel Sections Skip non-vowel characters to find valid ranges 2 Sliding Window Use two pointers to check all substrings 3 Track Vowel Set Count unique vowels in current window 4 Count Valid If all 5 vowels present, increment count Valid Substrings Found: "aeiou" "aeiouu" [0-4] and [0-5] FINAL RESULT Vowel Substrings Count 2 Substrings Found: 1. "aeiou" indices [0,4] - 5 chars 2. "aeiouu" indices [0,5] - 6 chars Output: 2 Key Insight: The optimized approach skips invalid sections (non-vowel characters) to avoid unnecessary checks. For each valid vowel-only section, we use sliding window to find substrings containing all 5 vowels. This reduces time complexity compared to brute force O(n^3) to a more efficient O(n^2) approach. TutorialsPoint - Count Vowel Substrings of a String | Optimized - Skip Invalid Sections
Asked in
Google 12 Amazon 8 Microsoft 6 Facebook 4
23.4K Views
Medium Frequency
~25 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