Optimize palindrome detection.