Skip to content

Commit 34d6d71

Browse files
authored
Merge pull request #1081 from 0xff-dev/2559
Add solution and test-cases for problem 2559
2 parents e585818 + fead394 commit 34d6d71

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [2559.Count Vowel Strings in Ranges][title]
2+
3+
## Description
4+
You are given a **0-indexed** array of strings `words` and a 2D array of integers `queries`.
5+
6+
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.
7+
8+
Return an array `ans` of size `queries.length`, where `ans[i]` is the answer to the `ith` query.
9+
10+
**Note** that the vowel letters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
16+
Output: [2,3,0]
17+
Explanation: The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
18+
The answer to the query [0,2] is 2 (strings "aba" and "ece").
19+
to query [1,4] is 3 (strings "ece", "aa", "e").
20+
to query [1,1] is 0.
21+
We return [2,3,0].
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
28+
Output: [3,2,1]
29+
Explanation: Every string satisfies the conditions, so we return [3,2,1].
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/count-vowel-strings-in-ranges
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func isVowel(b byte) bool {
4+
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u'
5+
}
6+
7+
func Solution(words []string, queries [][]int) []int {
8+
count := make([]int, len(words))
9+
pre := 0
10+
for i, w := range words {
11+
count[i] = pre
12+
if isVowel(w[0]) && isVowel(w[len(w)-1]) {
13+
count[i]++
14+
}
15+
pre = count[i]
16+
}
17+
ans := make([]int, len(queries))
18+
for i, q := range queries {
19+
ans[i] = count[q[1]]
20+
if q[0] > 0 {
21+
ans[i] -= count[q[0]-1]
22+
}
23+
}
24+
return ans
525
}

leetcode/2501-2600/2559.Count-Vowel-Strings-in-Ranges/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
words []string
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"aba", "bcb", "ece", "aa", "ee"}, [][]int{{0, 2}, {1, 4}, {1, 1}}, []int{2, 3, 0}},
18+
{"TestCase2", []string{"a", "e", "i"}, [][]int{{0, 2}, {0, 1}, {2, 2}}, []int{3, 2, 1}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.words, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.words, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)