Count of Substrings Containing Every Vowel and K Consonants II Problem
Description
LeetCode Problem 3306.
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.
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^5wordconsists only of lowercase English letters.0 <= k <= word.length - 5
Solution
Approach: At Least k, Minus At Least k + 1
Counting substrings with exactly k consonants is easier as a difference. Let f(k) be the number of substrings that contain all five vowels and at least k consonants. Then the answer is f(k) - f(k + 1).
To compute f(k), slide a window over word. Keep a map cnt of the vowels in the window, the number x of consonants, and the left end l. For each new character, add it to the window. While the window is valid (x >= k and all five vowels present), shrink it from the left. After the loop, the window [l, r] is no longer valid, but every start in [0, l - 1] gives a valid substring ending at r, so add l to the count.
Complexity: O(n) time and O(1) space, where n is the length of word.
class Solution:
def countOfSubstrings(self, word: str, k: int) -> int:
def f(k: int) -> int:
cnt = Counter()
ans = l = x = 0
for c in word:
if c in "aeiou":
cnt[c] += 1
else:
x += 1
while x >= k and len(cnt) == 5:
d = word[l]
if d in "aeiou":
cnt[d] -= 1
if cnt[d] == 0:
cnt.pop(d)
else:
x -= 1
l += 1
ans += l
return ans
return f(k) - f(k + 1)