LeetCode 3306: Count of Substrings Containing Every Vowel and K Consonants II Solution

Master LeetCode problem 3306 (Count of Substrings Containing Every Vowel and K Consonants II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3306. Count of Substrings Containing Every Vowel and K Consonants II

Problem Explanation

Explanation:

To solve this problem, we can use a sliding window approach. We will maintain a window of size at most 5 to ensure that all vowels are present in the window. We will then iterate through the string and keep track of the number of consonants inside the window. If the number of consonants is equal to k, we can calculate the number of substrings that satisfy the conditions and increment our count.

Algorithm:

  1. Initialize variables count and consonants to 0.
  2. Iterate through the input string using a sliding window approach.
  3. For each character:
    • If it is a vowel, mark it as present in a boolean array.
    • If it is a consonant, increment the consonants count.
    • If the size of the window is greater than 5, remove the first character from the window.
    • If the number of consonants in the window is greater than k, move the window start to the right until the number of consonants is less than or equal to k.
    • If all vowels are present in the window and the number of consonants is equal to k, increment the count by the size of the window.
  4. Return the final count.

Time Complexity:

The time complexity of this approach is O(n), where n is the length of the input string.

Space Complexity:

The space complexity is O(1) since we are using a constant amount of extra space.

:

Solution Code

class Solution {
    public int countSubstrings(String word, int k) {
        boolean[] vowels = new boolean[26];
        vowels['a' - 'a'] = true;
        vowels['e' - 'a'] = true;
        vowels['i' - 'a'] = true;
        vowels['o' - 'a'] = true;
        vowels['u' - 'a'] = true;
        
        int count = 0, consonants = 0;
        int[] cnt = new int[word.length()+1];
        
        int left = 0;
        for (int right = 0; right < word.length(); right++) {
            char c = word.charAt(right);
            if (vowels[c - 'a']) {
                cnt[right+1] = cnt[right];
            } else {
                consonants++;
                cnt[right+1] = cnt[right] + 1;
            }
            
            while (consonants > k) {
                if (!vowels[word.charAt(left) - 'a']) {
                    consonants--;
                }
                left++;
            }
            
            if (consonants == k) {
                count += right - left + 1;
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3306 (Count of Substrings Containing Every Vowel and K Consonants II)?

This page provides optimized solutions for LeetCode problem 3306 (Count of Substrings Containing Every Vowel and K Consonants II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3306 (Count of Substrings Containing Every Vowel and K Consonants II)?

The time complexity for LeetCode 3306 (Count of Substrings Containing Every Vowel and K Consonants II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3306 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3306 in Java, C++, or Python.

Back to LeetCode Solutions