Problem Description
Given a string, count how many substrings (contiguous sequences of characters) consist entirely of vowels and contain all five vowels at least once.
Key Insights
- Only vowels ('a', 'e', 'i', 'o', 'u') are allowed in a valid substring.
- The substring must include every one of the five vowels at least once.
- As you iterate the string, you can break early from an inner loop when a non-vowel is encountered.
- Given the constraint (word length up to 100), a nested loop approach is acceptable.
Space and Time Complexity
Time Complexity: O(n^3) in the worst-case scenario (when checking every substring and verifying vowel completeness), but practical performance is better due to early break on non-vowel characters.
Space Complexity: O(1) extra space, aside from input storage.
Solution
We use a brute force approach by iterating through all possible substrings. For each starting index, if the character is a vowel, iterate forward until a non-vowel character is encountered. Maintain a set of vowels seen so far as you extend the substring. When the set contains all five vowels, increment the count.
Key Data Structures:
- A set to track vowels in the current substring. Algorithm:
- Outer loop iterates over possible start indices.
- Inner loop generates substrings continuously as long as characters are vowels.
- For each valid substring, update the set and check if its size is 5.
- If true, increment the answer count.