NeetCode #90LC-2559MediumArrays & Hashing
← Back to All Problems#90 · #2559 · Count Vowel Strings in Ranges(统计范围内的元音字符串数)
📌 Problem Statement & Constraints
You are given an array of strings
words and an array of queries, each queries[i] = [l, r]. A string is a vowel string if it starts and ends with a vowel. For each query, return the number of vowel strings in words[l..r] inclusive. Constraints: 1 <= words.length <= 10^5, 1 <= queries.length <= 10^5.💡 Core Algorithmic Approaches
- Precompute a boolean per word indicating whether it is a vowel string.
- Build a prefix-sum array over those booleans so each range query is O(1).
- Answer each query as
prefix[r + 1] - prefix[l]. - This is the standard offline range-count pattern: O(n) preprocessing, O(1) per query.
💻 Benchmark Python3 Implementation
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
vowels = set("aeiou")
n = len(words)
pref = [0] * (n + 1)
for i, w in enumerate(words):
ok = 1 if (w[0] in vowels and w[-1] in vowels) else 0
pref[i + 1] = pref[i] + ok
return [pref[r + 1] - pref[l] for l, r in queries]⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n + q): O(n) to build the prefix array and O(1) per query.
💾 Space Complexity
O(n) for the prefix array.
⚠️ Interview Pitfalls & Follow-ups
- Recomputing the condition per query: O(n * q) and far too slow for the stated limits.
- Using
words[i][0] in 'aeiou': works, but a set lookup is clearer and avoids the substring-membership gotcha with multi-character strings. - Forgetting the leading zero in the prefix array: then
l == 0would need a special case. - Checking the length-1 word: a single-character word both starts and ends with that character, so the condition correctly evaluates to whether it is a vowel.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
统计范围内的元音字符串数
给你一个下标从 0 开始的字符串数组 words 以及一个二维整数数组 queries。每个查询 queries[i] = [li, ri] 会要求我们统计在 words 中下标在 li 到 ri 范围内(包含两端)并且以元音开头和结尾的字符串的数目。返回一个整数数组,其中第 i 个元素对应第 i 个查询的结果。元音字母为 a、e、i、o、u。约束:1 ≤ words.length ≤ 10^5;1 ≤ queries.length ≤ 10^5;1 ≤ words[i].length ≤ 40。
核心要点
- 前缀和(标准解) :预处理一个布尔数组标记每个单词是否「以元音开头且以元音结尾」,再对其做前缀和。
- 查询 O(1) : queries[i] 的答案是 prefix[ri + 1] - prefix[li] 。
- 为什么不能对每个查询重新扫描 :q 个查询、每个 O(n),总计 O(n · q) = 10^10,必然超时。
- 判定写法 : w[0] in "aeiou" and w[-1] in "aeiou" 。注意 w 非空(长度 ≥ 1),因此 w[0] 与 w[-1] 安全。
常见陷阱
- 对每个查询重新扫描区间 :O(n · q) 必然超时。必须用前缀和。
- 前缀和下标错位 : prefix[i] 表示「前 i 个」时,区间 [l, r] 的答案是 prefix[r+1] - prefix[l] 。写成 prefix[r] - prefix[l] 会漏掉 words[r]。
- 用字符串而非集合判断元音 : w[0] in "aeiou" 是子串判断,对单字符等价且更快;但若判断多字符就会出错。用集合更稳妥。
参考实现
class Solution:
def vowelStrings(self, words: list[str], queries: list[list[int]]) -> list[int]:
vowels = set('aeiou')
# prefix[i] = 前 i 个单词中「元音串」的个数
prefix = [0] * (len(words) + 1)
for i, w in enumerate(words):
ok = w[0] in vowels and w[-1] in vowels
prefix[i + 1] = prefix[i] + (1 if ok else 0)
return [prefix[r + 1] - prefix[l] for l, r in queries]