Skip to content

Commit 33185f4

Browse files
committed
Sync LeetCode submission Runtime - 19 ms (71.40%), Memory - 49.8 MB (30.11%)
1 parent 6b9e001 commit 33185f4

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a 2D array of integers <code>queries</code>.</p>
2+
3+
<p>Each query <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> asks us to find the number of strings present in the range <code>l<sub>i</sub></code> to <code>r<sub>i</sub></code> (both <strong>inclusive</strong>) of <code>words</code> that start and end with a vowel.</p>
4+
5+
<p>Return <em>an array </em><code>ans</code><em> of size </em><code>queries.length</code><em>, where </em><code>ans[i]</code><em> is the answer to the </em><code>i</code><sup>th</sup><em> query</em>.</p>
6+
7+
<p><strong>Note</strong> that the vowel letters are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> words = [&quot;aba&quot;,&quot;bcb&quot;,&quot;ece&quot;,&quot;aa&quot;,&quot;e&quot;], queries = [[0,2],[1,4],[1,1]]
14+
<strong>Output:</strong> [2,3,0]
15+
<strong>Explanation:</strong> The strings starting and ending with a vowel are &quot;aba&quot;, &quot;ece&quot;, &quot;aa&quot; and &quot;e&quot;.
16+
The answer to the query [0,2] is 2 (strings &quot;aba&quot; and &quot;ece&quot;).
17+
to query [1,4] is 3 (strings &quot;ece&quot;, &quot;aa&quot;, &quot;e&quot;).
18+
to query [1,1] is 0.
19+
We return [2,3,0].
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> words = [&quot;a&quot;,&quot;e&quot;,&quot;i&quot;], queries = [[0,2],[0,1],[2,2]]
26+
<strong>Output:</strong> [3,2,1]
27+
<strong>Explanation:</strong> Every string satisfies the conditions, so we return [3,2,1].</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code></li>
34+
<li><code>1 &lt;= words[i].length &lt;= 40</code></li>
35+
<li><code>words[i]</code> consists only of lowercase English letters.</li>
36+
<li><code>sum(words[i].length) &lt;= 3 * 10<sup>5</sup></code></li>
37+
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
38+
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;&nbsp;words.length</code></li>
39+
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Approach: Prefix Sum
2+
3+
# M = size of `words`, N = size of `queries`
4+
# Time: O(M + N)
5+
# Space: O(M)
6+
7+
class Solution:
8+
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
9+
ans = [0] * len(queries)
10+
vowels = ['a', 'e', 'i', 'o', 'u']
11+
prefix_sum = [0] * len(words)
12+
sum = 0
13+
14+
for i in range(len(words)):
15+
current_word = words[i]
16+
if (current_word[0] in vowels and current_word[-1] in vowels):
17+
sum += 1
18+
prefix_sum[i] = sum
19+
20+
for i in range(len(queries)):
21+
current_query = queries[i]
22+
ans[i] = prefix_sum[current_query[1]] - (0 if current_query[0] == 0 else prefix_sum[current_query[0] - 1])
23+
24+
return ans
25+
26+
27+

0 commit comments

Comments
 (0)