Overview
A palindrome reads the same forwards and backwards. Check by comparing the string to its reverse.
Analogy
'Racecar' — whether you start from the left or right, you read the same word.
Step-by-step
- Clean the string: lowercase, remove non-alphanumerics.
- Compare s == s[::-1].
- For manual check: two-pointer — compare s[left] and s[right], move inward.
Visual
'racecar' → reversed='racecar' → equal → palindrome
Common mistakes
- Forgetting to normalize case ('Racecar' ≠ 'racecar' before lower()).
- Not removing spaces/punctuation for sentence palindromes.
Practice questions
- Check 'A man a plan a canal Panama'.
- Find the longest palindromic substring.
Space
O(n) for slice, O(1) for two-pointer