Given a string S consisting of lowercase English characters, determine if you can make it a palindrome by removing at most 1 character.
Examples:
tacocats→true, because removing one character givestacocatabaabbaaba
Idea:
- Use two pointers, one from the left and one from the right.
- If the characters match, continue inward.
- If they do not match, try skipping either the left character or the right character.
- If either remaining substring is a palindrome, the answer is
true.
This problem is a classic two-pointer palindrome check with one allowed deletion. Scan from both ends, and when a mismatch appears, try skipping either the left character or the right character, then verify whether the remaining substring is a palindrome. The approach runs in O(n) time and turns the one-deletion constraint into two local palindrome checks.