Palindrome
Palindromes read the same forwards and backwards. These problems test string manipulation, two-pointer technique, and dynamic programming for substring/subsequence variations.Core Concepts
Palindrome Types
- String Palindrome: “racecar”, “level”
- Substring: Contiguous characters
- Subsequence: Non-contiguous characters (can skip)
- Numeric: 121, 12321
Key Differences
- Valid Palindrome: Check if string is palindrome (ignore non-alphanumeric)
- Longest Palindromic Substring: Contiguous, O(n²) or O(n) with Manacher’s
- Longest Palindromic Subsequence: Non-contiguous, O(n²) DP
Key Patterns & Techniques
1. Two Pointers
- Check from both ends moving inward
- Skip non-alphanumeric for validation
- O(n) time, O(1) space
2. Expand Around Center
- Try each position as center
- Expand while characters match
- Handle both odd and even length
- O(n²) time
3. Dynamic Programming
- dp[i][j] = is substring s[i:j+1] palindrome
- Build from smaller to larger substrings
- O(n²) time and space
4. Manacher’s Algorithm
- Linear time palindrome detection
- Transform string to handle even/odd
- O(n) time, O(n) space
Common Approaches
Valid Palindrome
Valid Palindrome II (One Delete Allowed)
Longest Palindromic Substring (Expand Around Center)
Palindromic Substrings (Count)
Valid Palindrome III (K Deletions)
Problems in This Category
029. Valid Palindrome
Easy | Frequency: 69.5%Two pointers ignoring non-alphanumeric characters.
004. Valid Palindrome II
Easy | Frequency: 92.7%Two pointers with one delete allowed - try both options on mismatch.
096. Longest Palindromic Substring
Medium | Frequency: 32.0%Expand around center for each position (odd and even length).
057. Palindromic Substrings
Medium | Frequency: 47.0%Count all palindromic substrings using expand around center.
083. Valid Palindrome III
Hard | Frequency: 40.7%DP to find longest palindromic subsequence, check if deletions ≤ k.
Complexity Characteristics
| Approach | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | Validation, simple check |
| Expand Around Center | O(n²) | O(1) | Find/count palindromic substrings |
| DP (2D) | O(n²) | O(n²) | Subsequence, multiple queries |
| Manacher’s Algorithm | O(n) | O(n) | Optimal substring finding |
Interview Tips
Pattern Recognition
- “Is valid palindrome?” → Two pointers from ends
- “At most k deletions” → DP for longest palindromic subsequence
- “Longest palindromic substring” → Expand around center
- “Count palindromic substrings” → Expand around each position
- “Minimum insertions/deletions” → DP similar to edit distance
Common Mistakes
- Not handling even and odd length palindromes separately
- Forgetting case-insensitive comparison
- Not skipping non-alphanumeric in validation
- Confusing substring (contiguous) with subsequence (non-contiguous)
- Off-by-one errors in string indices
Key Insights
- Two pointers for validation - O(n) time, O(1) space
- Expand around center - try each position as center
- Both odd and even - check s[i] as center and s[i:i+1] as center
- DP for subsequence - different from substring
- Manacher’s for optimal - O(n) but complex to implement
- LPS = Longest Palindromic Subsequence = LCS(s, reverse(s))
Expand Around Center Pattern
DP Pattern for Subsequence
Advanced Patterns
Manacher’s Algorithm
Palindrome Partitioning
Minimum Insertions to Make Palindrome
Pro Tip: For palindrome validation, use two pointers O(n). For finding longest palindromic substring, expand around each center O(n²). For subsequence problems (can skip characters), use DP O(n²). Remember to check both odd and even length palindromes.