To solve this problem efficiently, we'll use a sliding window approach combined with prefix sums and a hashmap to keep track of counts of consonant prefix sums within the current window.
Here's the step-by-step algorithm:
-
Initialize:
consonant_prefix_sum: An array to store the cumulative number of consonants up to each position.
last_occurrence: A dictionary to store the last occurrence index of each vowel.
counts: A hashmap (defaultdict) to store counts of consonant prefix sums within the current window.
prefix_sums_in_window: A deque to keep track of consonant prefix sums and their positions within the current window.
-
Process the string:
- Iterate over each character in the string.
- Update the
consonant_prefix_sum by adding 1 if the current character is a consonant.
- If the current character is a vowel, update its last occurrence index in
last_occurrence.
-
Determine the window's starting index L:
- Compute
L as the minimum of the last occurrences of all vowels. This ensures that the substring from L to the current position includes all vowels.
-
Maintain counts within the window:
- Remove prefix sums from
counts and prefix_sums_in_window if their positions are before L. This ensures we only consider starting positions within the current window.
-
Calculate the number of valid substrings ending at the current position:
- Compute the required sum (
required_sum) as consonant_prefix_sum[current] - k.
- Add the count of
required_sum in counts to the total number of substrings. This gives us the number of substrings starting within the window that have exactly k consonants.
-
Update counts:
- Add the current
consonant_prefix_sum to counts and append it to prefix_sums_in_window.
-
Edge Cases:
- Ensure to handle edge cases where
k is zero or when the substring starts at the beginning of the string.
-
Return the total count of substrings.
Here's the Python code implementing the above algorithm:
def count_substrings_with_all_vowels_and_k_consonants(word: str, k: int) -> int:
from collections import defaultdict, deque
N = len(word)
vowels = {'a', 'e', 'i', 'o', 'u'}
consonant_prefix_sum = [0] * (N + 1)
last_occurrence = {v: -1 for v in vowels}
counts = defaultdict(int)
prefix_sums_in_window = deque()
counts[0] = 1 # Empty prefix
prefix_sums_in_window.append((0, 0)) # (position, consonant_prefix_sum)
total_substrings = 0
for i in range(N):
# Update consonant_prefix_sum
consonant_prefix_sum[i + 1] = consonant_prefix_sum[i] + (0 if word[i] in vowels else 1)
# Update last occurrence of vowels
if word[i] in vowels:
last_occurrence[word[i]] = i
# Check if all vowels have been seen
if -1 in last_occurrence.values():
L = 0 # Not all vowels have been seen yet
else:
L = min(last_occurrence.values())
# Remove counts of positions before L
while prefix_sums_in_window and prefix_sums_in_window[0][0] < L:
old_pos, old_sum = prefix_sums_in_window.popleft()
counts[old_sum] -= 1
# Calculate required sum and update total substrings
required_sum = consonant_prefix_sum[i + 1] - k
total_substrings += counts.get(required_sum, 0)
# Update counts and prefix_sums_in_window
counts[consonant_prefix_sum[i + 1]] += 1
prefix_sums_in_window.append((i + 1, consonant_prefix_sum[i + 1]))
return total_substrings
# Example usage:
word = input().strip()
k = int(input())
result = count_substrings_with_all_vowels_and_k_consonants(word, k)
print(result)
This code will read the input word and integer k, compute the total number of substrings meeting the criteria, and print the result. The time complexity of this algorithm is O(N), making it efficient for large strings (up to (2 \times 10^5) characters).