From 4c5a83c578605fb27f7e8a26d18f5d465e5f90de Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 2 Jan 2025 10:16:15 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB2559.=20Count=20Vowel=20Strin?= =?UTF-8?q?gs=20in=20Ranges=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/count-vowel-strings-in-ranges/ Signed-off-by: Matej Focko --- go/count-vowel-strings-in-ranges.go | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 go/count-vowel-strings-in-ranges.go diff --git a/go/count-vowel-strings-in-ranges.go b/go/count-vowel-strings-in-ranges.go new file mode 100644 index 0000000..3409511 --- /dev/null +++ b/go/count-vowel-strings-in-ranges.go @@ -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 +}