Skip to content

S1Q2 · Starts and Ends with the Same Vowel

⚡ Quick Reference

Function: starts_and_ends_with_same_vowel(s: str) -> bool

Core idea: lowercase both ends, check both are vowels, and check they are the same letter.

def starts_and_ends_with_same_vowel(s: str) -> bool:
    vowels = set("aeiou")
    first  = s[0].lower()
    last   = s[-1].lower()
    return first in vowels and first == last

Key rules: - Case-insensitive → lowercase both characters - first in vowels ensures it's a vowel (also covers last since first == last) - Both must be the same vowel - equal after lowercasing


Problem Statement

Problem

Write a function starts_and_ends_with_same_vowel(s) that returns True if the string starts and ends with the same vowel, case-insensitively.

Examples:

Input
"Apple"
Output
False
Input
"Atta"
Output
True
Input
"Tart"
Output
False
Input
"umbrella"
Output
False

Tracing all examples

String first last Both vowels? Same? Result
"Apple" a e False
"Atta" a a True
"Tart" t t False
"umbrella" u a False

One vowel check is enough

Since we verify first == last, checking first in vowels is sufficient - if they're equal and the first is a vowel, the last must be the same vowel too. No need to check both separately.


Solution approaches

def starts_and_ends_with_same_vowel(s: str) -> bool:
    vowels = set("aeiou")
    first  = s[0].lower()
    last   = s[-1].lower()
    return first in vowels and first == last
def starts_and_ends_with_same_vowel(s: str) -> bool:
    vowels = "aeiou"
    first  = s[0].lower()
    last   = s[-1].lower()

    first_is_vowel = first in vowels
    last_is_vowel  = last in vowels
    same_letter    = first == last

    return first_is_vowel and last_is_vowel and same_letter
def starts_and_ends_with_same_vowel(s: str) -> bool:
    return s[0].lower() == s[-1].lower() and s[0].lower() in "aeiou"
starts_and_ends_with_same_vowel = lambda s: (
    s[0].lower() in "aeiou" and s[0].lower() == s[-1].lower()
)

Key takeaways

01

Lowercase both ends before comparing

s[0].lower() == s[-1].lower() ensures "Atta" correctly matches - 'A' and 'a' are the same vowel. Without lowercasing, the comparison fails for mixed-case strings.

02

One vowel check is sufficient

If first == last and first in vowels, then last must also be a vowel - the same one. Checking both ends for vowel membership is correct but redundant.

03

Same letter ≠ same vowel

"Tart" starts and ends with t/T - same letter, but not a vowel. The vowel check catches this. Always verify the character is in "aeiou", not just that the two ends match.