From 8a83a543ba4a6eefe855c428a0b64d75ffd4cf12 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 9 Nov 2021 16:40:44 +0000 Subject: [PATCH] count-vowel-substrings-of-a-string --- count-vowel-substrings-of-a-string/sol.go | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 count-vowel-substrings-of-a-string/sol.go diff --git a/count-vowel-substrings-of-a-string/sol.go b/count-vowel-substrings-of-a-string/sol.go new file mode 100644 index 0000000..b121447 --- /dev/null +++ b/count-vowel-substrings-of-a-string/sol.go @@ -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 +}