Question on Leetcode - Easy
The question states there are two integers that serve as boundaries for the array (left
and right
). Taking that into condideration, we are to only search for vowels in that boundary.
What qualifies a word to be a vowel string? It starts with a vowel character and ends with a vowel character. And the vowels in the English dictionary are: a
, e
, i
, o
, and u
.
To go about this, I defined a res
that would serve as a counter. Then run a for loop over the array with the range being the boundaries left
and right
. Then I check if the current word first letter and last letter starts with any vowel. If it does, add to counter res
and if it does not, pass.
class Solution:
def vowelStrings(self, words: List[str], left: int, right: int) -> int:
res = 0
for i in range(left, right + 1):
if words[i][0] in "aeiou" and words[i][-1] in "aeiou":
res += 1
return res
- Time Complexity: O(n) because of the boundary and array.
- Space Complexity: O(1).
res
is a constant space.