2025-05-04:元音辅音字符串计数Ⅱ。用go语言,你有一个字符串 word 和一个非负整数 k。
要求计算并返回 word 中所有满足以下条件的子字符串的数量:
1.子字符串中的每种元音字母('a'、'e'、'i'、'o'、'u')均至少出现过一次;
2.子字符串中辅音字母的总数正好是 k 个。
5 <= word.length <= 2 * 100000。
word 仅由小写英文字母组成。
0 <= k <= word.length - 5。
输入:word = "ieaouqqieaouqq", k = 1。
输出:3。
解释:
包含所有元音字母并且恰好含有一个辅音字母的子字符串有:
word[0..5],即 "ieaouq"。
word[6..11],即 "qieaou"。
word[7..12],即 "ieaouq"。
题目来自leetcode3306。
时间复杂度:O(n)。每个字符最多被i和j访问各一次,总体线性时间。
空间复杂度:O(1)。哈希表最多存储5个元音的出现次数,额外空间固定。
package main
import (
"fmt"
)
func countOfSubstrings(word string, k int)int64 {
vowels := map[byte]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true}
count := func(m int)int64 {
n := len(word)
var res int64 = 0
consonants := 0
occur := make(map[byte]int)
for i, j := 0, 0; i < n; i++ {
for j < n && (consonants < m || len(occur) < 5) {
if vowels[word[j]] {
occur[word[j]]++
} else {
consonants++
}
j++
}
if consonants >= m && len(occur) == 5 {
res += int64(n - j + 1)
}
if vowels[word[i]] {
occur[word[i]]--
if occur[word[i]] == 0 {
delete(occur, word[i])
}
} else {
consonants--
}
}
return res
}
return count(k) - count(k+1)
}
func main() {
word := "ieaouqqieaouqq"
k := 1
result := countOfSubstrings(word, k)
fmt.Println(result)
}
# -*-coding:utf-8-*-
defcount_of_substrings(word: str, k: int) -> int:
vowels = {'a', 'e', 'i', 'o', 'u'}
defcount(m: int) -> int:
n = len(word)
res = 0
consonants = 0
occur = {}
j = 0
for i inrange(n):
while j < n and (consonants < m orlen(occur) < 5):
if word[j] in vowels:
occur[word[j]] = occur.get(word[j], 0) + 1
else:
consonants += 1
j += 1
if consonants >= m andlen(occur) == 5:
res += n - j + 1
# 缩小左边界
if word[i] in vowels:
occur[word[i]] -= 1
if occur[word[i]] == 0:
del occur[word[i]]
else:
consonants -= 1
return res
return count(k) - count(k + 1)
if __name__ == "__main__":
word = "ieaouqqieaouqq"
k = 1
result = count_of_substrings(word, k)
print(result)