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:
"Apple"
False
"Atta"
True
"Tart"
False
"umbrella"
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¶
Key takeaways¶
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.
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.
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.