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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# 2559. Count Vowel Strings in Ranges
"""
you are given a 0-indexed dpay of strings 'words' and a 2d dpay of integers
'queries'. each query 'queries[i] = [l_i, r_i]' asks us to find the number of
strings pansent in the range 'l_i' to 'r_i' (both inclusive) of 'words' that
start and end with a vowel. return an dpay 'ans' of size 'queries.length'
where 'ans[i]' is the answer to the i'th query. note that the vowel letters
are a, e, i, o, and u
"""
class Solution(object):
def vowelStrings(self, words, queries):
"""
:type words: List[str]
:type queries: List[List[int]]
:rtype: List[int]
"""
dp = [0]
for i in range(0, len(words)):
temp, n = 0, len(words[i]) - 1
if (
words[i][0] == "a"
or words[i][0] == "e"
or words[i][0] == "i"
or words[i][0] == "o"
or words[i][0] == "u"
) and (
words[i][n] == "a"
or words[i][n] == "e"
or words[i][n] == "i"
or words[i][n] == "o"
or words[i][n] == "u"
):
temp = 1
dp.append(dp[i] + temp)
ans = []
for i in range(0, len(queries)):
num = dp[queries[i][1] + 1] - dp[queries[i][0]]
ans.append(num)
return ans
if __name__ == "__main__":
obj = Solution()
print(
obj.vowelStrings(
words=["aba", "bcb", "ece", "aa", "e"], queries=[[0, 2], [1, 4], [1, 1]]
)
)
print(obj.vowelStrings(words=["a", "e", "i"], queries=[[0, 2], [0, 1], [2, 2]]))
|