From fead394c6425dbd71c05ae51b700f969bdc93238 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 2 Jan 2025 09:12:47 +0800 Subject: [PATCH] Add solution and test-cases for problem 2559 --- .../README.md | 37 +++++++++++++++++++ .../Solution.go | 24 +++++++++++- .../Solution_test.go | 22 +++++------ 3 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/README.md diff --git a/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/README.md b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/README.md new file mode 100644 index 000000000..d1b49c3af --- /dev/null +++ b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/README.md @@ -0,0 +1,37 @@ +# [2559.Count Vowel Strings in Ranges][title] + +## Description +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]. +``` + +**Example 2:** + +``` +Input: words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]] +Output: [3,2,1] +Explanation: Every string satisfies the conditions, so we return [3,2,1]. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-vowel-strings-in-ranges +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution.go b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution.go index d115ccf5e..b1a047854 100755 --- a/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution.go +++ b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +func isVowel(b byte) bool { + return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' +} + +func Solution(words []string, queries [][]int) []int { + count := make([]int, len(words)) + pre := 0 + for i, w := range words { + count[i] = pre + if isVowel(w[0]) && isVowel(w[len(w)-1]) { + count[i]++ + } + pre = count[i] + } + ans := make([]int, len(queries)) + for i, q := range queries { + ans[i] = count[q[1]] + if q[0] > 0 { + ans[i] -= count[q[0]-1] + } + } + return ans } diff --git a/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution_test.go b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution_test.go index 14ff50eb4..ce474674b 100755 --- a/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution_test.go +++ b/leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + words []string + queries [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"aba", "bcb", "ece", "aa", "ee"}, [][]int{{0, 2}, {1, 4}, {1, 1}}, []int{2, 3, 0}}, + {"TestCase2", []string{"a", "e", "i"}, [][]int{{0, 2}, {0, 1}, {2, 2}}, []int{3, 2, 1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.words, c.queries) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.words, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }