Explain count-vowel-substrings-of-a-string
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
_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
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 250"><g stroke="currentColor"><rect x="40" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/><rect x="96" y="60" width="56" height="44" fill="none"/><rect x="152" y="60" width="56" height="44" fill="none"/><rect x="208" y="60" width="56" height="44" fill="none"/><rect x="264" y="60" width="56" height="44" fill="none"/><rect x="320" y="60" width="56" height="44" fill="#3b82f6" fill-opacity="0.35"/><rect x="376" y="60" width="56" height="44" fill="none"/><rect x="432" y="60" width="56" height="44" fill="none"/><rect x="488" y="60" width="56" height="44" fill="none"/><rect x="544" y="60" width="56" height="44" fill="none"/></g><g font-family="ui-monospace, monospace" font-size="18" fill="currentColor" text-anchor="middle"><text x="68" y="88">c</text><text x="124" y="88">u</text><text x="180" y="88">a</text><text x="236" y="88">i</text><text x="292" y="88">e</text><text x="348" y="88">u</text><text x="404" y="88">o</text><text x="460" y="88">u</text><text x="516" y="88">a</text><text x="572" y="88">c</text></g><g font-family="ui-monospace, monospace" font-size="13" fill="currentColor" text-anchor="middle"><text x="68" y="124">0</text><text x="124" y="124">1</text><text x="180" y="124">2</text><text x="236" y="124">3</text><text x="292" y="124">4</text><text x="348" y="124">5</text><text x="404" y="124">6</text><text x="460" y="124">7</text><text x="516" y="124">8</text><text x="572" y="124">9</text></g><g font-family="sans-serif" font-size="14" fill="currentColor" text-anchor="middle"><text x="124" y="45">start</text><text x="348" y="45">j</text></g><text x="40" y="180" font-family="ui-monospace, monospace" font-size="15" fill="currentColor">last: a=2 e=4 i=3 o=-1 u=5</text><text x="40" y="215" font-family="sans-serif" font-size="15" fill="currentColor">o missing: +0, total = 0</text></svg>
|
||||
```
|
||||
|
||||
**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
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 250"><g stroke="currentColor"><rect x="40" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/><rect x="96" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="152" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="208" y="60" width="56" height="44" fill="none"/><rect x="264" y="60" width="56" height="44" fill="none"/><rect x="320" y="60" width="56" height="44" fill="none"/><rect x="376" y="60" width="56" height="44" fill="#3b82f6" fill-opacity="0.35"/><rect x="432" y="60" width="56" height="44" fill="none"/><rect x="488" y="60" width="56" height="44" fill="none"/><rect x="544" y="60" width="56" height="44" fill="none"/></g><g font-family="ui-monospace, monospace" font-size="18" fill="currentColor" text-anchor="middle"><text x="68" y="88">c</text><text x="124" y="88">u</text><text x="180" y="88">a</text><text x="236" y="88">i</text><text x="292" y="88">e</text><text x="348" y="88">u</text><text x="404" y="88">o</text><text x="460" y="88">u</text><text x="516" y="88">a</text><text x="572" y="88">c</text></g><g font-family="ui-monospace, monospace" font-size="13" fill="currentColor" text-anchor="middle"><text x="68" y="124">0</text><text x="124" y="124">1</text><text x="180" y="124">2</text><text x="236" y="124">3</text><text x="292" y="124">4</text><text x="348" y="124">5</text><text x="404" y="124">6</text><text x="460" y="124">7</text><text x="516" y="124">8</text><text x="572" y="124">9</text></g><g font-family="sans-serif" font-size="14" fill="currentColor" text-anchor="middle"><text x="124" y="45">start</text><text x="404" y="45">j</text><text x="180" y="146">min</text></g><text x="40" y="180" font-family="ui-monospace, monospace" font-size="15" fill="currentColor">last: a=2 e=4 i=3 o=6 u=5</text><text x="40" y="215" font-family="sans-serif" font-size="15" fill="currentColor">m=2: +2, total = 2</text></svg>
|
||||
```
|
||||
|
||||
**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
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 250"><g stroke="currentColor"><rect x="40" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/><rect x="96" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="152" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="208" y="60" width="56" height="44" fill="none"/><rect x="264" y="60" width="56" height="44" fill="none"/><rect x="320" y="60" width="56" height="44" fill="none"/><rect x="376" y="60" width="56" height="44" fill="none"/><rect x="432" y="60" width="56" height="44" fill="#3b82f6" fill-opacity="0.35"/><rect x="488" y="60" width="56" height="44" fill="none"/><rect x="544" y="60" width="56" height="44" fill="none"/></g><g font-family="ui-monospace, monospace" font-size="18" fill="currentColor" text-anchor="middle"><text x="68" y="88">c</text><text x="124" y="88">u</text><text x="180" y="88">a</text><text x="236" y="88">i</text><text x="292" y="88">e</text><text x="348" y="88">u</text><text x="404" y="88">o</text><text x="460" y="88">u</text><text x="516" y="88">a</text><text x="572" y="88">c</text></g><g font-family="ui-monospace, monospace" font-size="13" fill="currentColor" text-anchor="middle"><text x="68" y="124">0</text><text x="124" y="124">1</text><text x="180" y="124">2</text><text x="236" y="124">3</text><text x="292" y="124">4</text><text x="348" y="124">5</text><text x="404" y="124">6</text><text x="460" y="124">7</text><text x="516" y="124">8</text><text x="572" y="124">9</text></g><g font-family="sans-serif" font-size="14" fill="currentColor" text-anchor="middle"><text x="124" y="45">start</text><text x="460" y="45">j</text><text x="180" y="146">min</text></g><text x="40" y="180" font-family="ui-monospace, monospace" font-size="15" fill="currentColor">last: a=2 e=4 i=3 o=6 u=7</text><text x="40" y="215" font-family="sans-serif" font-size="15" fill="currentColor">m=2: +2, total = 4</text></svg>
|
||||
```
|
||||
|
||||
**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
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 250"><g stroke="currentColor"><rect x="40" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/><rect x="96" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="152" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="208" y="60" width="56" height="44" fill="#22c55e" fill-opacity="0.35"/><rect x="264" y="60" width="56" height="44" fill="none"/><rect x="320" y="60" width="56" height="44" fill="none"/><rect x="376" y="60" width="56" height="44" fill="none"/><rect x="432" y="60" width="56" height="44" fill="none"/><rect x="488" y="60" width="56" height="44" fill="#3b82f6" fill-opacity="0.35"/><rect x="544" y="60" width="56" height="44" fill="none"/></g><g font-family="ui-monospace, monospace" font-size="18" fill="currentColor" text-anchor="middle"><text x="68" y="88">c</text><text x="124" y="88">u</text><text x="180" y="88">a</text><text x="236" y="88">i</text><text x="292" y="88">e</text><text x="348" y="88">u</text><text x="404" y="88">o</text><text x="460" y="88">u</text><text x="516" y="88">a</text><text x="572" y="88">c</text></g><g font-family="ui-monospace, monospace" font-size="13" fill="currentColor" text-anchor="middle"><text x="68" y="124">0</text><text x="124" y="124">1</text><text x="180" y="124">2</text><text x="236" y="124">3</text><text x="292" y="124">4</text><text x="348" y="124">5</text><text x="404" y="124">6</text><text x="460" y="124">7</text><text x="516" y="124">8</text><text x="572" y="124">9</text></g><g font-family="sans-serif" font-size="14" fill="currentColor" text-anchor="middle"><text x="124" y="45">start</text><text x="516" y="45">j</text><text x="236" y="146">min</text></g><text x="40" y="180" font-family="ui-monospace, monospace" font-size="15" fill="currentColor">last: a=8 e=4 i=3 o=6 u=7</text><text x="40" y="215" font-family="sans-serif" font-size="15" fill="currentColor">m=3: +3, total = 7</text></svg>
|
||||
```
|
||||
|
||||
**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
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 250"><g stroke="currentColor"><rect x="40" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/><rect x="96" y="60" width="56" height="44" fill="none"/><rect x="152" y="60" width="56" height="44" fill="none"/><rect x="208" y="60" width="56" height="44" fill="none"/><rect x="264" y="60" width="56" height="44" fill="none"/><rect x="320" y="60" width="56" height="44" fill="none"/><rect x="376" y="60" width="56" height="44" fill="none"/><rect x="432" y="60" width="56" height="44" fill="none"/><rect x="488" y="60" width="56" height="44" fill="none"/><rect x="544" y="60" width="56" height="44" fill="#ef4444" fill-opacity="0.35"/></g><g font-family="ui-monospace, monospace" font-size="18" fill="currentColor" text-anchor="middle"><text x="68" y="88">c</text><text x="124" y="88">u</text><text x="180" y="88">a</text><text x="236" y="88">i</text><text x="292" y="88">e</text><text x="348" y="88">u</text><text x="404" y="88">o</text><text x="460" y="88">u</text><text x="516" y="88">a</text><text x="572" y="88">c</text></g><g font-family="ui-monospace, monospace" font-size="13" fill="currentColor" text-anchor="middle"><text x="68" y="124">0</text><text x="124" y="124">1</text><text x="180" y="124">2</text><text x="236" y="124">3</text><text x="292" y="124">4</text><text x="348" y="124">5</text><text x="404" y="124">6</text><text x="460" y="124">7</text><text x="516" y="124">8</text><text x="572" y="124">9</text></g><g font-family="sans-serif" font-size="14" fill="currentColor" text-anchor="middle"><text x="572" y="45">j</text></g><text x="40" y="180" font-family="ui-monospace, monospace" font-size="15" fill="currentColor">last: all -1, start = 10</text><text x="40" y="215" font-family="sans-serif" font-size="15" fill="currentColor">consonant reset: answer = 7</text></svg>
|
||||
```
|
||||
|
||||
## 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
|
||||
```
|
||||
Reference in New Issue
Block a user