-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_vowelStrings.py
More file actions
29 lines (29 loc) · 1.21 KB
/
02_vowelStrings.py
File metadata and controls
29 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
''' Time Complexity O(n)
You are given a 0-indexed array of strings words and a 2D array of integers queries.
Each query queries[i] = [li, ri] asks us to find the number of strings present in the range
li to ri (both inclusive) of words that start and end with a vowel.
Return an array ans of size queries.length, where ans[i] is the answer to the ith query.
Note that the vowel letters are 'a', 'e', 'i', 'o', and 'u'.
Example 1:
Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
Output: [2,3,0]
Explanation: The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
The answer to the query [0,2] is 2 (strings "aba" and "ece").
to query [1,4] is 3 (strings "ece", "aa", "e").
to query [1,1] is 0.
We return [2,3,0].
'''
from typing import *
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
vowel=['a','e','i','o','u']
preFix=[0]
res=[]
count=0
for i in words:
if i[0] in vowel and i[len(i)-1] in vowel:
count+=1
preFix.append(count)
for i in queries:
res.append(preFix[i[1]+1]-preFix[i[0]])
return res