Valid Palindrome II
Given a string s, return True if s can be a palindrome after deleting at most one character.
Example
aba
True
- Input:
sis the string "aba", a sequence of lowercase letters indexed . - A string is a palindrome when every character equals its mirror: for all . For the only mirrored pair is positions and ; the middle letter at position pairs with itself.
- Here a, so the pair matches and "aba" is already a palindrome.
- Deletions used: , which satisfies "at most one", so the output is
True. - Contrast: "abca" has a mismatched inner pair (b vs c), yet deleting the b leaves "aca" and deleting the c leaves "aba"; more than one deletion can work, so it is still
True. "abc" mismatches (a vs c), and every one-letter deletion ("bc", "ac", "ab") leaves a non-palindrome, so it isFalse. - Notice that the output only says whether some deletion of at most one letter works, not which letter; and the middle letter of an odd-length string never has to match anything.
Constraints
- 1 <= len(s) <= 10^5
- s consists of lowercase English letters