LeetCode 3305: Count of Substrings Containing Every Vowel and K Consonants I Solution
Master LeetCode problem 3305 (Count of Substrings Containing Every Vowel and K Consonants I), 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.
3305. Count of Substrings Containing Every Vowel and K Consonants I
Problem Explanation
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:
- 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. - Iterate over the string using two pointers (left and right).
- For each character, update the hashmap and increment the vowels and consonants count accordingly.
- If the current window contains every vowel and
kconsonants, increment the total valid substrings count. - Slide the window by moving the left pointer and update counts accordingly.
- 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.
:
Solution Code
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;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3305 (Count of Substrings Containing Every Vowel and K Consonants I)?
This page provides optimized solutions for LeetCode problem 3305 (Count of Substrings Containing Every Vowel and K Consonants I) 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 3305 (Count of Substrings Containing Every Vowel and K Consonants I)?
The time complexity for LeetCode 3305 (Count of Substrings Containing Every Vowel and K Consonants I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3305 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3305 in Java, C++, or Python.