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: Check each string in the list to see if it is a palindrome using a helper function.
1) Define a helper function is_palindrome(s):
a) Initialize two pointers, left starting at the beginning of the string, and right at the end.
b) Loop while left is less than right:
i) If characters at left and right are not the same, return False (not a palindrome).
ii) Move left pointer one step to the right.
iii) Move right pointer one step to the left.
c) If no mismatches are found, return True (it is a palindrome).
2) Define the main function first_palindrome(words):
a) Loop through each word in the list:
i) Use the is_palindrome function to check if the current word is a palindrome.
ii) If a palindrome is found, return that word immediately.
b) If no palindromes are found after checking all words, return an empty string.
⚠️ Common Mistakes
def is_palindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
def first_palindrome(words):
for word in words:
if is_palindrome(word):
return word
return "