go: add «2559. Count Vowel Strings in Ranges»
URL: https://leetcode.com/problems/count-vowel-strings-in-ranges/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
8ecf9fb8d8
commit
4c5a83c578
1 changed files with 38 additions and 0 deletions
38
go/count-vowel-strings-in-ranges.go
Normal file
38
go/count-vowel-strings-in-ranges.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import "slices"
|
||||
|
||||
func vowelStrings(words []string, queries [][]int) []int {
|
||||
isVowel := func(c byte) bool {
|
||||
return slices.Contains([]byte{'a', 'e', 'i', 'o', 'u'}, c)
|
||||
}
|
||||
|
||||
makePrefixCount := func() []int {
|
||||
prefixCount := make([]int, len(words))
|
||||
|
||||
count := 0
|
||||
for i, word := range words {
|
||||
if isVowel(word[0]) && isVowel(word[len(word)-1]) {
|
||||
count++
|
||||
}
|
||||
prefixCount[i] = count
|
||||
}
|
||||
|
||||
return prefixCount
|
||||
}
|
||||
prefixCount := makePrefixCount()
|
||||
|
||||
answer := make([]int, len(queries))
|
||||
for i, query := range queries {
|
||||
till := prefixCount[query[1]]
|
||||
|
||||
preceding := 0
|
||||
if query[0] != 0 {
|
||||
preceding = prefixCount[query[0]-1]
|
||||
}
|
||||
|
||||
answer[i] = till - preceding
|
||||
}
|
||||
|
||||
return answer
|
||||
}
|
Loading…
Reference in a new issue