count-vowel-substrings-of-a-string

This commit is contained in:
Gleb Koval 2021-11-09 16:40:44 +00:00
parent d6844d9388
commit 8a83a543ba
No known key found for this signature in database
GPG Key ID: DF27F6A77C48FDA0
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package main
func vowel(char rune) (int, bool) {
v, ok := map[rune]int{
'a': 0b00001,
'e': 0b00010,
'i': 0b00100,
'o': 0b01000,
'u': 0b10000,
}[char]
return v, ok
}
func countVowelSubstrings(word string) int {
order := make([]int, 0)
counts := make(map[int]int)
last, m, count := 0, 1, 0
for _, char := range word {
if last == 0b11111 {
count += m
}
vow, ok := vowel(char)
if !ok {
order = make([]int, 0)
counts = make(map[int]int)
last, m = 0, 1
} else {
for len(order) > 0 && (order[0] == vow || counts[order[0]] > 1) {
counts[order[0]]--
order = order[1:]
m++
}
last |= vow
order = append(order, vow)
counts[vow]++
}
}
if last == 0b11111 {
count += m
}
return count
}