-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution2586.go
51 lines (45 loc) · 1.37 KB
/
solution2586.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
51
package solution2586
// ============================================================================
// 2586. Count the Number of Vowel Strings in Range
// URL: https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2586---Count-the-Number-of-Vowel-Strings-in-Range
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_vowelStrings-24 315741366 3.793 ns/op 0 B/op 0 allocs/op
Benchmark_vowelStrings_bitmask-24 745920466 1.608 ns/op 0 B/op 0 allocs/op
*/
func vowelStrings(words []string, left int, right int) int {
isVowel := func(ch byte) bool {
switch {
case ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u':
return true
}
return false
}
ans := 0
for i := 0; i < len(words); i++ {
if i >= left && i <= right {
word := words[i]
a := word[0]
b := word[len(word)-1]
if isVowel(a) && isVowel(b) {
ans++
}
}
}
return ans
}
func vowelStrings_bitmask(words []string, left int, right int) int {
ans := 0
vowels := 0b_100000100000100010001
for i := left; i <= right; i++ {
if vowels&(1<<(words[i][0]-'a')) > 0 && vowels&(1<<(words[i][len(words[i])-1]-'a')) > 0 {
ans++
}
}
return ans
}