> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count of Substrings Containing Every Vowel

> Tested Python solution for LeetCode 3306 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 3306, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3306   # by problem number
lcpy gen -s count_of_substrings_containing_every_vowel_and_k_consonants_ii   # by problem name
```

## Problem

You are given a string \<code>word\</code> and a \<strong>non-negative\</strong> integer \<code>k\</code>.

Return the total number of \<span data-keyword="substring-nonempty">substrings\</span> of \<code>word\</code> that contain every vowel (\<code>'a'\</code>, \<code>'e'\</code>, \<code>'i'\</code>, \<code>'o'\</code>, and \<code>'u'\</code>) \<strong>at least\</strong> once and \<strong>exactly\</strong> \<code>k\</code> consonants.

### Examples

```
Input: word = "aeioqq", k = 1
Output: 0
Explanation: There is no substring with every vowel.
```

```
Input: word = "aeiou", k = 0
Output: 1
Explanation: The only substring with every vowel and zero consonants is word[0..4], which is "aeiou".
```

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_of_substrings_containing_every_vowel_and_k_consonants_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_of_substrings_containing_every_vowel_and_k_consonants_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def count_of_substrings(self, word: str, k: int) -> int:
        def at_least(min_k: int) -> int:
            vowel_counts: dict[str, int] = {}
            consonants = 0
            total = 0
            left = 0
            for right, ch in enumerate(word):
                if ch in "aeiou":
                    vowel_counts[ch] = vowel_counts.get(ch, 0) + 1
                else:
                    consonants += 1
                while len(vowel_counts) == 5 and consonants >= min_k:
                    total += len(word) - right
                    left_ch = word[left]
                    if left_ch in "aeiou":
                        vowel_counts[left_ch] -= 1
                        if vowel_counts[left_ch] == 0:
                            del vowel_counts[left_ch]
                    else:
                        consonants -= 1
                    left += 1
            return total

        return at_least(k) - at_least(k + 1)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.