Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

3305. Count of Substrings Containing Every Vowel and K Consonants I

Explanation:

To solve this problem, we can use a sliding window approach to iterate through the string and count the number of vowels and consonants in each substring. We can maintain two pointers for the window - left and right. We will move the right pointer to expand the window and count vowels and consonants. If we find a substring containing every vowel and k consonants, we increment the count of valid substrings. We continue this process until we reach the end of the string.

Algorithmic Idea:

  1. Initialize variables for vowels (aeiou) count, consonants count, total valid substrings count, and a hashmap to keep track of the characters in the current window.
  2. Iterate over the string using two pointers (left and right).
  3. For each character, update the hashmap and increment the vowels and consonants count accordingly.
  4. If the current window contains every vowel and k consonants, increment the total valid substrings count.
  5. Slide the window by moving the left pointer and update counts accordingly.
  6. Return the total valid substrings count.

Time Complexity:

The time complexity of this solution is O(N) where N is the length of the input string word.

Space Complexity:

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

:

class Solution {
    public int countSubstrings(String word, int k) {
        int count = 0;
        int vowels = 0;
        int consonants = 0;
        int[] map = new int[26];

        for (char c : word.toCharArray()) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                vowels++;
            } else {
                consonants++;
            }
        }

        if (vowels < 5 || consonants < k) {
            return 0;
        }

        int left = 0;
        for (int right = 0; right < word.length(); right++) {
            char c = word.charAt(right);
            map[c - 'a']++;

            while (vowels >= 5 && consonants >= k) {
                if (map['a' - 'a'] > 0 && map['e' - 'a'] > 0 && map['i' - 'a'] > 0 && map['o' - 'a'] > 0 && map['u' - 'a'] > 0 && consonants == k) {
                    count += word.length() - right;
                }
                char leftChar = word.charAt(left);
                map[leftChar - 'a']--;
                if (leftChar == 'a' || leftChar == 'e' || leftChar == 'i' || leftChar == 'o' || leftChar == 'u') {
                    vowels--;
                } else {
                    consonants--;
                }
                left++;
            }
        }

        return count;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.