TIP102 Unit 3 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
landmarks, where each string represents the name of a landmark.".Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the array of landmark names and check each one to see if it is a palindrome. Return the first palindrome found, or return an empty string if no palindrome exists.
1. Define a helper function `is_palindrome(landmark)` that checks whether a given string is a palindrome:
1. Initialize two pointers, `left` at the start of the string and `right` at the end.
2. While `left` is less than `right`, compare the characters at these positions:
* If the characters are different, return `False`.
* If the characters are the same, move `left` one step to the right and `right` one step to the left.
3. If all characters match, return `True`.
2. Iterate through the `landmarks` array:
1. For each landmark, use `is_palindrome` to check if it is a palindrome.
2. If a palindrome is found, return that landmark.
3. If no palindrome is found after iterating through all landmarks, return an empty string `"`.
⚠️ Common Mistakes
def is_palindrome(landmark):
left, right = 0, len(landmark) - 1
while left < right:
if landmark[left] != landmark[right]:
return False
left += 1
right -= 1
return True
def first_symmetrical_landmark(landmarks):
for landmark in landmarks:
if is_palindrome(landmark):
return landmark
return ""
# Example usage
print(first_symmetrical_landmark(["canyon","forest","rotor","mountain"])) # Output: "rotor"
print(first_symmetrical_landmark(["plateau","valley","cliff"])) # Output: ""
