aboutsummaryrefslogtreecommitdiff
path: root/count_vowel_string_range.py
diff options
context:
space:
mode:
Diffstat (limited to 'count_vowel_string_range.py')
-rw-r--r--count_vowel_string_range.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/count_vowel_string_range.py b/count_vowel_string_range.py
new file mode 100644
index 0000000..692618a
--- /dev/null
+++ b/count_vowel_string_range.py
@@ -0,0 +1,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]]))