Palindrom String
Recursion
📋 Problem Statement
Write a recursive function is_palindrome(s) that returns True if the string reads the same forward and backward, and False otherwise. Input: "racecar" → True. Input: "hello" → False. You must use recursion.
💡 Intuition
Check if , then recursively check the inner substring . The base case is when the pointers cross () — a single character or empty string is always a palindrome.
🔧 Approach
- Pass
leftandrightpointer indices along with the string. - Base case: if
left >= right, return True. - If
s[left] != s[right], return False immediately. - Otherwise, recurse with
left + 1andright - 1.
⚡ Complexity Analysis
Time
— at most recursive calls, each doing work
Space
for the recursion call stack ( frames)
⚠️ Common Mistakes
- Not passing the left and right pointers, trying to slice the string instead
- Using
left > rightas the base case instead ofleft >= right(misses the single-character case) - Forgetting to handle case sensitivity if required
- Creating new string slices at each level, adding per call
🎯 Final Thoughts
This shows how the same algorithm (two-pointer palindrome check) can be expressed iteratively ( space) or recursively ( space). The recursive "check boundaries, recurse inward" pattern also appears in binary search and divide-and-conquer.