forked from codepath/compsci_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
2 Pointer Palindrome
Jessica Sang edited this page Sep 14, 2024
·
1 revision
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.
- What if the string is empty?
- An empty string is considered a palindrome as it reads the same forward and backward.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use two pointers to compare characters from the beginning and end of the string moving towards the center.
1) Initialize two pointers, left at the start (0) and right at the end (length of s - 1)
2) While left pointer is less than right pointer:
a) If characters at left and right pointers do not match, return False
b) Increment left pointer and decrement right pointer
3) If all characters match, return True
- Not handling strings of different lengths properly.
- Incorrectly initializing the pointers.
def is_palindrome(s):
left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True