_Explained by claude-opus-5-5 on 20 October 2026._ ## Key observation Fix the right end `j`. A left end `l` works iff `l` is past the last consonant and every vowel's most recent occurrence is $\ge l$. So the valid left ends form one contiguous range whose length you can read off directly. ## Hint 1 A valid substring can never contain a consonant. What does that tell you about how the string splits into independent pieces? ## Hint 2 Instead of fixing where a substring *starts*, try fixing where it *ends*. For a fixed right end `j`, which left ends work? ## Hint 3 For a left end `l` and right end `j` inside one vowel block, `[l, j]` contains vowel `v` iff the most recent occurrence of `v` at or before `j` is at index $\ge l$. ## Hint 4 So the valid left ends form one contiguous range: from the start of the current vowel block up to $\min_v last[v]$. Add the length of that range for every `j`. ## Approach ## Why your two-pointer idea is close but slippery Your plan fixes the **left** end `i`. As soon as `[i, j]` contains all five vowels, you add every extension `[i, j'] ` with $j' \ge j$. Then you drop `word[i]` and advance. The idea is sound, but three details usually break it: 1. **The extensions stop at the next consonant, not at the end of the string.** `[i, j']` is only valid while `word[j']` is still a vowel. So you add `(end of vowel block) - j`, not `n - j`. 2. **After dropping `word[i]`, the window may *still* contain all five vowels.** For example, in `"aaeiou"`, dropping the first `a` leaves `"aeiou"`. You must keep shrinking and adding in a **loop** while the window stays complete, before you move `j` again. Doing it only once undercounts. 3. **A consonant resets everything.** No valid substring can cross a consonant, so `i` jumps past it and all counts clear. With those fixes your version works. The bookkeeping gets much easier if you flip the viewpoint and fix the **right** end instead. ## Fix the right end, count the left ends For each index `j`, ask: *how many valid substrings end exactly at `j`?* Summing that over all `j` counts every substring once, with no double counting. Track two things while scanning: - `start` is the first index after the most recent consonant. Every left end must be $\ge$ `start`. - `last[v]` is the most recent index (in the current vowel block) where vowel `v` appeared, or $-1$ if `v` hasn't appeared since `start`. A substring `[l, j]` contains vowel `v` exactly when `last[v] >= l`. It contains all five exactly when $$l \le m = \min_{v \in \{a,e,i,o,u\}} last[v].$$ Combined with $l \ge start$, the valid left ends are exactly $start, start+1, \dots, m$. That gives $$\text{contribution of } j = \max(0,\; m - start + 1).$$ When `word[j]` is a consonant, set `start = j + 1` and reset every `last[v]` to $-1$. Any missing vowel then makes $m = -1 < start$, so the contribution is automatically 0. This is a sliding window in disguise. `start` is the hard left wall, and $m$ is the furthest the left pointer could move while keeping all five vowels. You never move a pointer back and forth, you just read off the count. *(With $n \le 100$, a plain double loop also passes. For each `i`, extend `j` until a consonant, keeping a set of seen vowels and adding 1 whenever the set has size 5. But the idea above is the clean $O(n)$ version.)* ## Walkthrough **Step 1.** `word = "cuaieuouac"`. The `c` at index 0 forces `start = 1`. Scanning up to `j = 5` records `last`: a=2, e=4, i=3, u=5, but `o` hasn't appeared yet ($-1$). So $m = -1 < start$ and nothing is added. ```svg cuaieuouac0123456789startjlast: a=2 e=4 i=3 o=-1 u=5o missing: +0, total = 0 ``` **Step 2.** `j = 6` (`o`). Now all five vowels are present. `last`: a=2, e=4, i=3, o=6, u=5, so $m = 2$. Valid left ends are 1..2 (green), giving `"uaieuo"` and `"aieuo"`. Add $2 - 1 + 1 = 2$. ```svg cuaieuouac0123456789startjminlast: a=2 e=4 i=3 o=6 u=5m=2: +2, total = 2 ``` **Step 3.** `j = 7` (`u`). `last[u]` becomes 7, but the minimum is still `a` at index 2, so $m = 2$. Left ends 1..2 still work: +2, total 4. ```svg cuaieuouac0123456789startjminlast: a=2 e=4 i=3 o=6 u=7m=2: +2, total = 4 ``` **Step 4.** `j = 8` (`a`). `last[a]` jumps to 8, so the bottleneck becomes `i` at index 3 and $m = 3$. Left ends 1..3 are valid: +3, total 7. ```svg cuaieuouac0123456789startjminlast: a=8 e=4 i=3 o=6 u=7m=3: +3, total = 7 ``` **Step 5.** `j = 9` (`c`). A consonant resets `start = 10` and clears `last`. Nothing more can be added, so the answer is **7**, matching the expected output. ```svg cuaieuouac0123456789jlast: all -1, start = 10consonant reset: answer = 7 ``` ## Complexity - **Time:** $O(n)$. Each character is processed once, with a constant 5-way min. - **Memory:** $O(1)$. Only five `last` slots plus `start` and the counter. ## Pitfalls - Counting extensions up to the end of the string instead of up to the next consonant. - Shrinking the left pointer only once after finding a complete window. It must loop while the window still has all five vowels (`"aaeiou"` exposes this). - Forgetting to reset the vowel tracking when a consonant appears. Stale positions from an earlier block would produce substrings that cross the consonant. - Tracking vowels in a window with a plain *set* and then removing a vowel when the left pointer passes it, even though another copy is still inside. Use counts, or last-seen indices. - Not guarding against a missing vowel. With `last = -1` and the check `m >= start`, it naturally adds 0. ## Solution sketch ```text count = 0 start = 0 last = {a:-1, e:-1, i:-1, o:-1, u:-1} for j from 0 to n-1: ch = word[j] if ch is not a vowel: start = j + 1 set every last[v] = -1 continue last[ch] = j m = min(last[a], last[e], last[i], last[o], last[u]) if m >= start: count += m - start + 1 return count ```