Count vowel-only substrings with all vowels
Company: Expedia
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Count vowel-only substrings with all vowels states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= |s| <= 200000
- s contains only lowercase English letters
- Consonants split s into maximal vowel-only segments; only vowel-only substrings can qualify
- A valid substring must contain each of a, e, i, o, u at least once
- Target: O(n) time, O(1) extra space
Examples
Input: "aeiou"
Expected Output: 1
Explanation: Only the whole string contains all five vowels.
Input: "aeiouu"
Expected Output: 2
Explanation: Valid substrings are 'aeiou' (chars 0-4) and 'aeiouu' (chars 0-5).
Hints
- Consonants can never appear in a valid substring, so first split s into maximal runs of vowels and solve each run independently.
- Within one vowel run, for a fixed right end, count the number of valid left starts. As right moves forward, the smallest valid left only moves forward too (monotonic) — that is the two-pointer invariant.
- Track how many DISTINCT vowels the current window has using a size-5 frequency table. While all 5 are present, advance left as far as possible; the count of valid starts for this right is (left - segmentStart).
- Use a 64-bit accumulator: with up to 200000 characters the answer can exceed 32-bit range.