Unit 4 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use two pointers to identify letters from both ends of the string and swap them until they meet in the middle.
1) Define a helper function `is_letter` to determine if a character is an alphabetic letter.
2) Convert the string into a list of characters to facilitate in-place swaps.
3) Initialize two pointers, `left` at the beginning and `right` at the end of the list.
4) While the `left` pointer is less than the `right` pointer:
a) Move the `left` pointer rightward until it points to a letter.
b) Move the `right` pointer leftward until it points to a letter.
c) If both pointers point to letters, swap them and then move both pointers towards the center.
5) Convert the list back to a string and return it.
⚠️ Common Mistakes
# Helper function to check if a character is an English letter
def is_letter(c):
return c.isalpha()
def reverse_only_letters(s):
# Convert the string into a list of characters for easy manipulation
chars = list(s)
left, right = 0, len(chars) - 1
while left < right:
if not is_letter(chars[left]):
left += 1
elif not is_letter(chars[right]):
right -= 1
else:
# Swap the characters
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
# Convert the list of characters back to a string
return ''.join(chars)