S2Q1 · Replace Vowels with Next Alphabet¶
⚡ Quick Reference
Function: replace_vowels_with_next_alphabet(s: str) -> str
Core idea: walk through each character - if it's a vowel, replace it with chr(ord(c) + 1), otherwise keep it.
def replace_vowels_with_next_alphabet(s: str) -> str:
vowels = "aeiouAEIOU"
return "".join(chr(ord(c) + 1) if c in vowels else c for c in s)
Key rules:
- Both lowercase and uppercase vowels are replaced
- chr(ord(c) + 1) gives the next character in the alphabet
- Case is preserved - 'a'→'b', 'A'→'B'
- Non-vowel characters are unchanged
Problem Statement¶
Problem
Write a function replace_vowels_with_next_alphabet(s: str) -> str that replaces every vowel in s with the next letter in the alphabet. The function is case-sensitive - lowercase vowels become the next lowercase letter, uppercase vowels become the next uppercase letter.
Examples:
"hello"
'hflmp'
"HELLO"
'HFLMP'
Understanding the problem¶
Two things to decide for each character:
| Character type | Action |
|---|---|
Vowel (a e i o u A E I O U) |
Replace with chr(ord(c) + 1) |
| Everything else | Keep unchanged |
How chr() and ord() work together
Every character has a numeric ASCII code. ord('a') = 97, ord('b') = 98, and so on.
ord(c)→ get the ASCII number of charactercord(c) + 1→ next number in the sequencechr(...)→ convert back to a character
So chr(ord('e') + 1) = chr(101 + 1) = chr(102) = 'f'.
Tracing the examples¶
"hello"
| Char | Vowel? | Operation | Result |
|---|---|---|---|
h |
❌ | keep | h |
e |
✅ | chr(ord('e')+1) = chr(102) |
f |
l |
❌ | keep | l |
l |
❌ | keep | l |
o |
✅ | chr(ord('o')+1) = chr(112) |
p |
Result: "hfllp"
Wait - the expected output is 'hflmp', not 'hfllp'
Let's re-trace more carefully:
| Char | Vowel? | Result |
|---|---|---|
h |
❌ | h |
e |
✅ | f |
l |
❌ | l |
l |
❌ | l |
o |
✅ | p |
That gives "hfllp", not "hflmp". However the expected output in the template is 'hflmp'. This suggests the second l is also being treated as a vowel-like replacement, which is unusual.
Testing in Python: chr(ord('l') + 1) = 'm'. So it appears all consonants may also be shifted, not just vowels. But the problem statement says "replace each vowel" - this is likely a mistake in the example. The correct implementation per the problem description replaces only vowels.
Implement as stated: replace vowels only.
Solution approaches¶
def replace_vowels_with_next_alphabet(s: str) -> str:
vowels = "aeiouAEIOU"
result = ""
for c in s:
if c in vowels:
result += chr(ord(c) + 1) # next character in alphabet
else:
result += c # keep unchanged
return result
Walk through each character explicitly. Most readable for beginners.
def replace_vowels_with_next_alphabet(s: str) -> str:
vowels = "aeiouAEIOU"
return "".join(
chr(ord(c) + 1) if c in vowels else c
for c in s
)
Generator expression inside "".join() - the standard Pythonic pattern for building a transformed string. Preferred in interviews.
def replace_vowels_with_next_alphabet(s: str) -> str:
table = str.maketrans("aeiouAEIOU", "bfjpvBFJPV")
return s.translate(table)
str.maketrans builds a translation table mapping each vowel to its replacement. str.translate applies it in one pass. The most efficient approach for fixed character mappings - no ord/chr needed, just a lookup table.
The replacement characters: a→b, e→f, i→j, o→p, u→v (and uppercase equivalents).
def replace_vowels_with_next_alphabet(s: str) -> str:
mapping = {v: chr(ord(v) + 1) for v in "aeiouAEIOU"}
return "".join(mapping.get(c, c) for c in s)
Build a dict of vowel → next char, then use dict.get(c, c) - returns the mapped value if c is a vowel, or c itself (the default) if it's not. Clean and extensible.
def replace_vowels_with_next_alphabet(s: str) -> str:
vowels = "aeiouAEIOU"
return "".join(map(
lambda c: chr(ord(c) + 1) if c in vowels else c,
s
))
map(lambda c: ..., s) applies the transformation to every character in s. The lambda decides: if vowel → shift by 1, otherwise keep. "".join() reassembles the result. A clean functional alternative to the generator expression approach.
Key takeaways¶
ord() and chr()
ord(c) gets the ASCII value of a character. chr(n) converts back. Together they let you shift characters arithmetically - chr(ord(c) + 1) is the next character.
"".join() for string building
Never build strings by concatenating in a loop (result += c) in production - it's O(n²). Use "".join(generator) instead. It's O(n) and more Pythonic.
str.translate() for character mapping
When you have a fixed mapping of characters, str.maketrans() + str.translate() is the fastest and cleanest approach - no loops needed at all.