#2559
Count Vowel Strings in Ranges
MediumApproaches
💡
Intuition
Time O(n²)Space O(1)
The brute force approach checks each string in the specified range for every query. This is straightforward but inefficient, especially for large inputs.
⚙️
Algorithm
5 steps- 1Step 1: For each query, extract the range [l, r].
- 2Step 2: Loop through the strings in the range from l to r.
- 3Step 3: For each string, check if it starts and ends with a vowel.
- 4Step 4: Count the number of strings that satisfy the condition.
- 5Step 5: Store the count for the current query.
solution.py13 lines
1# Full working Python code
2words = ["aba", "bcb", "ece", "aa", "e"]
3queries = [[0, 2], [1, 4], [1, 1]]
4vowels = {'a', 'e', 'i', 'o', 'u'}
5
6ans = []
7for l, r in queries:
8 count = 0
9 for i in range(l, r + 1):
10 if words[i][0] in vowels and words[i][-1] in vowels:
11 count += 1
12 ans.append(count)
13print(ans)ℹ
Complexity note: The time complexity is O(n²) because for each query, we may check up to n strings, leading to a quadratic number of checks in the worst case.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.