Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created July 6, 2023 15:40
Show Gist options
  • Save Ifihan/deac7abba7781113d0cd1359be0f0997 to your computer and use it in GitHub Desktop.
Save Ifihan/deac7abba7781113d0cd1359be0f0997 to your computer and use it in GitHub Desktop.
Count the Number of Vowel Strings in Range

Count the Number of Vowel Strings in Range

Question on Leetcode - Easy

Approach

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.

Code

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

Complexities

  • Time Complexity: O(n) because of the boundary and array.
  • Space Complexity: O(1). res is a constant space.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment