-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2062-CountVowelSubstringsOfAString.go
More file actions
121 lines (109 loc) · 3.2 KB
/
2062-CountVowelSubstringsOfAString.go
File metadata and controls
121 lines (109 loc) · 3.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
// 2062. Count Vowel Substrings of a String
// A substring is a contiguous (non-empty) sequence of characters within a string.
// A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it.
// Given a string word, return the number of vowel substrings in word.
// Example 1:
// Input: word = "aeiouu"
// Output: 2
// Explanation: The vowel substrings of word are as follows (underlined):
// - "aeiouu"
// - "aeiouu"
// Example 2:
// Input: word = "unicornarihan"
// Output: 0
// Explanation: Not all 5 vowels are present, so there are no vowel substrings.
// Example 3:
// Input: word = "cuaieuouac"
// Output: 7
// Explanation: The vowel substrings of word are as follows (underlined):
// - "cuaieuouac"
// - "cuaieuouac"
// - "cuaieuouac"
// - "cuaieuouac"
// - "cuaieuouac"
// - "cuaieuouac"
// - "cuaieuouac"
// Constraints:
// 1 <= word.length <= 100
// word consists of lowercase English letters only.
import "fmt"
import "strings"
// sliding window
func countVowelSubstrings(word string) int {
res := 0
for i := 0; i < len(word); i++ {
check := make(map[byte]int)
for j := i; j < len(word); j++ {
if strings.Contains("aeiou", string(word[j])) {
check[word[j]]++
} else {
break
}
if len(check) == 5 {
res++
}
}
}
return res
}
func countVowelSubstrings1(word string) int {
arr := []string{}
for i := 0; i < len(word); i++ {
j := i
for j < len(word) && strings.ContainsRune("aeiou", rune(word[j])) { j++ }
if j > i {
arr = append(arr, word[i:j])
}
i = j
}
containsAll := func (count [128]int) bool {
for _, v := range "aeiou" {
if count[v] == 0 { return false }
}
return true
}
res := 0
for _, s := range arr {
count := [128]int{}
l, r := 0, 0
for ; r < len(s); r++ {
count[s[r]]++
for l < r && containsAll(count) {
count[s[l]]--
l++
}
res += l
}
}
return res
}
func main() {
// Example 1:
// Input: word = "aeiouu"
// Output: 2
// Explanation: The vowel substrings of word are as follows (underlined):
// - "[aeiou]u"
// - "[aeiouu]"
fmt.Println(countVowelSubstrings("aeiouu")) // 2
// Example 2:
// Input: word = "unicornarihan"
// Output: 0
// Explanation: Not all 5 vowels are present, so there are no vowel substrings.
fmt.Println(countVowelSubstrings("unicornarihan")) // 0
// Example 3:
// Input: word = "cuaieuouac"
// Output: 7
// Explanation: The vowel substrings of word are as follows (underlined):
// - "c[uaieuo]uac"
// - "c[uaieuou]ac"
// - "c[uaieuoua]c"
// - "cu[aieuo]uac"
// - "cu[aieuou]ac"
// - "cu[aieuoua]c"
// - "cua[ieuoua]c"
fmt.Println(countVowelSubstrings("cuaieuouac")) // 0
fmt.Println(countVowelSubstrings1("aeiouu")) // 2
fmt.Println(countVowelSubstrings1("unicornarihan")) // 0
fmt.Println(countVowelSubstrings1("cuaieuouac")) // 0
}