aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-01-02 12:36:30 +0100
committerJack Sangdahl <[email protected]>2025-01-02 12:36:30 +0100
commit0d437ad50acc4103a9316281c5a020e6773cf5d5 (patch)
treef36d4fcd7b1102cdd75bcf347ab46e09fffd9e81
parent3fcaa644e9bc20851329227b01d496ab1318ca18 (diff)
downloadleetcode-0d437ad50acc4103a9316281c5a020e6773cf5d5.tar.gz
leetcode-0d437ad50acc4103a9316281c5a020e6773cf5d5.zip
2559. count vowel strings in ranges
-rw-r--r--count_vowel_string_range.c55
-rw-r--r--count_vowel_string_range.py52
2 files changed, 107 insertions, 0 deletions
diff --git a/count_vowel_string_range.c b/count_vowel_string_range.c
new file mode 100644
index 0000000..57eda96
--- /dev/null
+++ b/count_vowel_string_range.c
@@ -0,0 +1,55 @@
+// 2559. Count Vowel Strings in Ranges
+#include "leetcode.h"
+
+/*
+ * you are given a 0-indexed array of strings 'words' and a 2d array of integers
+ * 'queries'. each query 'queries[i] = [l_i, r_i]' asks us to find the number of
+ * strings present in the range 'l_i' to 'r_i' (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 i'th query. note that the vowel letters
+ * are a, e, i, o, and u
+ */
+
+int *vowelStrings(char **words, int wordsSize, int **queries, int queriesSize,
+ int *queriesColSize, int *returnSize) {
+ int *dp = (int *)malloc((wordsSize + 1) * sizeof(int));
+ dp[0] = 0;
+ for (int i = 0; i < wordsSize; i++) {
+ int tmp = 0, n = strlen(words[i]) - 1;
+ if (words[i][0] == 'a' || words[i][0] == 'e' || words[i][0] == 'i' ||
+ words[i][0] == 'o' || words[i][0] == 'u')
+ if (words[i][n] == 'a' || words[i][n] == 'e' || words[i][n] == 'i' ||
+ words[i][n] == 'o' || words[i][n] == 'u')
+ tmp = 1;
+ dp[i + 1] = dp[i] + tmp;
+ }
+ int *ans = (int *)malloc(queriesSize * sizeof(int));
+ for (int i = 0; i < queriesSize; i++) {
+ int num = dp[queries[i][i] + 1] - dp[queries[i][0]];
+ ans[i] = num;
+ }
+ *returnSize = queriesSize;
+ free(dp);
+ return ans;
+}
+
+int main() {
+ char *w1[] = {"aba", "bcb", "ece", "aa", "e"}, *w2[] = {"a", "e", "i"};
+ int q1i[3][2] = {{0, 2}, {1, 4}, {1, 1}}, rs1;
+ int q2i[3][2] = {{0, 2}, {0, 1}, {2, 2}}, rs2;
+ struct two_d_arr q1, q2;
+ two_d_arr_init(&q1, 3, 2, q1i);
+ two_d_arr_init(&q2, 3, 2, q2i);
+ int *vs1 = vowelStrings((char **)w1, ARRAY_SIZE(w1), q1.arr, q1.row_size,
+ q1.col_size, &rs1);
+ int *vs2 = vowelStrings((char **)w2, ARRAY_SIZE(w2), q2.arr, q2.row_size,
+ q2.col_size, &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", vs1[i]); // expect: 2 3 0
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", vs2[i]); // expect: 3 2 1
+ printf("\n");
+ free(vs1), free(vs2);
+ two_d_arr_free(&q1), two_d_arr_free(&q2);
+}
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]]))