leetcode solution 680 Valid Palindrome II

Given a non-empty string s
, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
1 | Input: "aba" |
Example 2:
1 | Input: "abca" |
Note:
- The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
1 | class Solution { |
- The basic idea is to compare the first ant the last character of
string s
. if s is a palindrome after remove one single letter. we can know that it can be seen as insert a letter into a palindrome. - so, we can consitently compare the first and the last letter of
s
, if they’re not the same, we judge the two substring which is the original string removing one of the ends letter.aabbcaa
: we consistently judgeabbca
->bbc
->bb
->ba