-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (103 loc) · 2.66 KB
/
main.go
File metadata and controls
114 lines (103 loc) · 2.66 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
// Source: https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii
// Title: Count of Substrings Containing Every Vowel and K Consonants II
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string `word` and a **non-negative** integer `k`.
//
// Return the total number of substrings of `word` that contain every vowel (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) **at least** once and **exactly** `k` consonants.
//
// A **substring** is a contiguous non-empty sequence of characters within a string.
//
// **Example 1:**
//
// ````
// Input: word = "aeioqq", k = 1
// Output: 0
// Explanation:
// There is no substring with every vowel.
// ```
//
// **Example 2:**
//
// ````
// Input: word = "aeiou", k = 0
// Output: 1
// Explanation:
// The only substring with every vowel and zero consonants is `word[0..4]`, which is `"aeiou"`.
// ````
//
// **Example 3:**
//
// ````
// Input: word = "ieaouqqieaouqq", k = 1
// Output: 3
// Explanation:
// The substrings with every vowel and one consonant are:
// - `word[0..5]`, which is `"ieaouq"`.
// - `word[6..11]`, which is `"qieaou"`.
// - `word[7..12]`, which is `"ieaouq"`.
// ````
//
// **Constraints:**
//
// - `5 <= word.length <= 2 * 10^5`
// - `word` consists only of lowercase English letters.
// - `0 <= k <= word.length - 5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// Sliding Windows
func countOfSubstrings(word string, k int) int64 {
n := len(word)
res := int64(0)
vowels := make(map[byte]int, 5)
consonants := int(0)
isVowel := func(c byte) bool {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
}
nextConsonants := make([]int, n)
nextIdx := n
for i := n - 1; i >= 0; i-- {
nextConsonants[i] = nextIdx
if !isVowel(word[i]) {
nextIdx = i
}
}
start, end := 0, 0
for end < n {
endChar := word[end]
if isVowel(endChar) {
vowels[endChar]++
} else {
consonants++
}
for consonants > k {
startChar := word[start]
if isVowel(startChar) {
vowels[startChar]--
if vowels[startChar] == 0 {
delete(vowels, startChar)
}
} else {
consonants--
}
start++
}
for start < n && len(vowels) == 5 && consonants == k {
res += int64(nextConsonants[end] - end)
startChar := word[start]
if isVowel(startChar) {
vowels[startChar]--
if vowels[startChar] == 0 {
delete(vowels, startChar)
}
} else {
consonants--
}
start++
}
end++
}
return res
}