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 * 105
word
consists only of lowercase English letters.0 <= k <= word.length - 5
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves checking every single possible substring of the given string. For each substring, we determine if it meets the criteria of containing all vowels and a specific number of consonants, counting the ones that do.
Here's how the algorithm would work step-by-step:
def count_substrings(input_string, required_consonants):
string_length = len(input_string)
substring_count = 0
for start_index in range(string_length):
for end_index in range(start_index, string_length):
current_substring = input_string[start_index : end_index + 1]
vowel_count = 0
consonant_count = 0
vowels = "aeiou"
#Check if all vowels and required consonants
for char in current_substring:
if char in vowels:
vowel_count += 1
elif 'a' <= char <= 'z':
consonant_count += 1
# Check if the substring contains all vowels
all_vowels_present = all(vowel in current_substring for vowel in vowels)
#Check the vowel and consonant requirements
if all_vowels_present and consonant_count == required_consonants:
substring_count += 1
return substring_count
The most efficient way to solve this problem involves a focused search through the string, rather than checking every single substring. We use a 'sliding window' approach combined with tracking to determine valid substrings more quickly. This way, we don't have to check every substring from scratch.
Here's how the algorithm would work step-by-step:
def count_substrings(input_string, required_consonants):
string_length = len(input_string)
vowels = "aeiou"
total_substring_count = 0
for left in range(string_length):
vowel_presence = {'a': False, 'e': False, 'i': False, 'o': False, 'u': False}
consonant_count = 0
for right in range(left, string_length):
character = input_string[right]
if character in vowels:
vowel_presence[character] = True
else:
consonant_count += 1
all_vowels_present = all(vowel_presence.values())
# Check if the substring is valid.
if all_vowels_present and consonant_count >= required_consonants:
# Shrink the window from the left.
temp_left = left
while temp_left <= right:
temp_character = input_string[temp_left]
temp_vowel_presence = vowel_presence.copy()
temp_consonant_count = consonant_count
if temp_character in vowels:
temp_vowel_presence[temp_character] = False
if not all(temp_vowel_presence.values()):
total_substring_count += (string_length - right)
break
else:
temp_consonant_count -= 1
if temp_consonant_count < required_consonants:
total_substring_count += (string_length - right)
break
total_substring_count += (string_length - right)
temp_left += 1
break
return total_substring_count
Case | How to Handle |
---|---|
Null or empty input string | Return 0, as no substrings can be formed from an empty string. |
String length less than 5 (number of vowels) | Return 0, because a substring needs at least one of each vowel. |
String contains only vowels (k > 0 is impossible) | If k > 0, return 0, as no consonants exist. |
String contains no vowels | Return 0, because no substring can contain all vowels. |
k = 0 and string contains all vowels at least once | Count all substrings, as any substring containing all vowels satisfies the condition. |
Large input string causing potential integer overflow in count | Use 64-bit integer type (long in Java, long long in C++) for counting substrings. |
Input string contains non-alphabetic characters | Ignore non-alphabetic characters or throw an exception, depending on requirements. |
Maximum input string length, leading to performance issues with naive O(n^2) solution | Optimize using sliding window or other more efficient algorithms with O(n) time complexity. |