0
0
DSA Pythonprogramming

Palindrome Detection in DSA Python

Choose your learning style9 modes available
Mental Model
Check if a sequence reads the same forwards and backwards by comparing characters from both ends moving towards the center.
Analogy: Like checking if a word on a mirror looks the same on both sides, such as 'madam' or 'racecar'.
start -> [m] a d a m ← end
indexes: 0 -> 1 -> 2 -> 3 -> 4
Dry Run Walkthrough
Input: string: 'radar'
Goal: Determine if 'radar' reads the same forwards and backwards
Step 1: Compare first and last characters 'r' and 'r'
r a d a r
↑           ↑
left=0     right=4
Why: Check if characters at both ends match to confirm palindrome property
Step 2: Move inward and compare 'a' and 'a'
r a d a r
  ↑     ↑
left=1 right=3
Why: Continue checking matching pairs moving towards the center
Step 3: Move inward to middle character 'd'
r a d a r
    ↑
left=2 right=2
Why: Middle character doesn't need a pair, so palindrome confirmed
Result:
r a d a r
Palindrome: True
Annotated Code
DSA Python
class PalindromeDetector:
    def is_palindrome(self, s: str) -> bool:
        left, right = 0, len(s) - 1
        while left < right:
            if s[left] != s[right]:
                return False
            left += 1
            right -= 1
        return True

if __name__ == '__main__':
    detector = PalindromeDetector()
    test_string = 'radar'
    result = detector.is_palindrome(test_string)
    print(f"{test_string} -> Palindrome: {result}")
while left < right:
loop to compare characters from both ends moving inward
if s[left] != s[right]:
check if characters at current pointers differ, which breaks palindrome
left += 1 right -= 1
advance pointers inward to next characters for comparison
return True
all pairs matched, confirm string is palindrome
OutputSuccess
radar -> Palindrome: True
Complexity Analysis
Time: O(n) because we compare pairs of characters from both ends once, where n is string length
Space: O(1) because only two pointers are used regardless of input size
vs Alternative: Compared to reversing the string and comparing, this method uses less space and stops early on mismatch
Edge Cases
empty string
returns True since empty string reads the same forwards and backwards
DSA Python
while left < right:
single character string
returns True because a single character is always a palindrome
DSA Python
while left < right:
string with different characters
returns False as soon as a mismatch is found
DSA Python
if s[left] != s[right]:
When to Use This Pattern
When you need to check if a sequence reads the same forwards and backwards, use two pointers moving inward to compare pairs efficiently.
Common Mistakes
Mistake: Comparing characters only from start to middle without checking the corresponding end character
Fix: Use two pointers, one at start and one at end, and compare both simultaneously
Mistake: Reversing the string and comparing without considering extra space
Fix: Use two-pointer approach to save space and allow early exit on mismatch
Summary
Checks if a string reads the same forwards and backwards by comparing characters from both ends.
Use when you want to verify if a word or sequence is a palindrome efficiently.
The key insight is to use two pointers moving inward to compare pairs, stopping early if mismatch occurs.