-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-vowel-strings-in-ranges.go
50 lines (46 loc) · 1.09 KB
/
count-vowel-strings-in-ranges.go
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
package study2023_06
func vowelStrings(words []string, queries [][]int) []int {
counts := make([]int, len(words)+1)
counts[0] = 0
cnt := 0
for i, word := range words {
if isVowelWord(word) {
cnt++
}
counts[i+1] = cnt
}
res := make([]int, 0)
for _, query := range queries {
res = append(res, counts[query[1]+1]-counts[query[0]])
}
return res
}
func isVowelWord(word string) bool {
vowelList := []string{"a", "e", "i", "o", "u"}
if contains(vowelList, string(word[0])) &&
contains(vowelList, string(word[len(word)-1])) {
return true
}
return false
}
func contains(list []string, str string) bool {
for _, item := range list {
if item == str {
return true
}
}
return false
}
func VowelStringsTest() {
// words := []string{"aba", "bcb", "ece", "aa", "e"}
// queries = [[0,2],[1,4],[1,1]]
// queries := [][]int{{0, 2}, {1, 4}, {1, 1}}
//words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
words := []string{"a", "e", "i"}
queries := [][]int{{0, 2}, {0, 1}, {2, 2}}
res := vowelStrings(words, queries)
// print res
for _, item := range res {
print(item, " ")
}
}