This document explains the solution to the "Count Vowel Strings in Ranges" problem in five languages: C++, Java, JavaScript, Python, and Go.
Given a list of strings and multiple queries, determine how many strings in each range of queries start and end with a vowel.
-
Initialize Vowels Set:
Create a set of vowels (a, e, i, o, u
) for quick lookup. -
Prepare Prefix Array:
Use a prefix array to precompute the cumulative count of strings that start and end with vowels.- If the current word starts and ends with a vowel, increment the prefix count at the current index.
- Add the previous prefix value to maintain cumulative counts.
-
Handle Queries:
For each query[l, r]
, calculate the difference between prefix sums:- If
l > 0
: Useprefix[r] - prefix[l-1]
. - Otherwise: Simply use
prefix[r]
.
- If
-
Return Results:
Store the results for all queries in a vector and return them.
-
Initialize Vowels Set:
Use Java'sSet.of()
to store the vowels (a, e, i, o, u
) for fast lookups. -
Build Prefix Array:
- Create an array
prefix
to store cumulative counts. - Check if each string starts and ends with a vowel.
- Update the prefix sum:
- Add 1 if valid, and carry forward the previous sum otherwise.
- Create an array
-
Process Queries:
Loop through each query and calculate the result using the prefix array:- For
l > 0
, use the difference betweenprefix[r]
andprefix[l-1]
. - Otherwise, use
prefix[r]
directly.
- For
-
Output Results:
Store the results for all queries in an array and return it.
-
Define Vowels Set:
Use aSet
in JavaScript for vowels (a, e, i, o, u
) to enable fast lookups. -
Compute Prefix Array:
- Initialize an array
prefix
to store cumulative counts. - Iterate through the list of strings:
- If the string starts and ends with a vowel, increment the count at the current index.
- Add the value from the previous prefix index to maintain cumulative counts.
- Initialize an array
-
Answer Queries:
Use the prefix array to compute the result for each query:- Subtract
prefix[l-1]
fromprefix[r]
whenl > 0
. - If
l == 0
, directly useprefix[r]
.
- Subtract
-
Return Results:
Store and return the results as an array.
-
Create a Vowel Set:
Use aset
to store the vowels (a, e, i, o, u
) for quick membership checks. -
Generate Prefix Array:
- Initialize a
prefix
list with zeros. - For each word:
- Check if it starts and ends with a vowel.
- If valid, increment the prefix value at the current index.
- Add the value from the previous index to maintain cumulative counts.
- Initialize a
-
Process Queries:
For each query:- If
l > 0
, calculate the differenceprefix[r] - prefix[l-1]
. - Otherwise, use
prefix[r]
.
- If
-
Return the Results:
Collect the results for all queries in a list and return.
-
Define Vowel Map:
Use amap
with vowels (a, e, i, o, u
) as keys for quick lookups. -
Compute Prefix Array:
- Initialize an array
prefix
to store cumulative counts. - Check if each word starts and ends with a vowel:
- Increment the count if valid.
- Add the previous prefix value to maintain cumulative sums.
- Initialize an array
-
Answer Queries:
For each query:- Subtract
prefix[l-1]
fromprefix[r]
whenl > 0
. - If
l == 0
, directly useprefix[r]
.
- Subtract
-
Output Results:
Store and return the results for all queries in a slice.
-
Optimization with Prefix Sums:
Using a prefix sum array reduces the time complexity of handling multiple range queries. Instead of checking strings in each query range repeatedly, we preprocess the counts once. -
Edge Cases:
Handle cases where the range starts at the beginning of the list (l == 0
) or if there are no valid strings in a query range.
- Precomputing the prefix sum:
$$O(n)$$ - Answering queries:
$$O(m)$$ - Total:
$$O(n + m)$$ , where (n) is the number of strings, and (m) is the number of queries.
- Prefix array:
$$O(n)$$ - Additional data structures (constant):
$$O(1)$$ - Total:
$$O(n)$$ .