题目
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Example 1:
Input: s = “aba”
Output: true
Example 2:
Input: s = “abca”
Output: true
Explanation: You could delete the character ‘c’.
Example 3:
Input: s = “abc”
Output: false
Constraints:
- 1 <= s.length <= 10^5
- s consists of lowercase English letters.
代码
class Solution {
public:
bool validPalindrome(string s) {
if(s.length() < 1)
return true;
for(int i = 0, j = s.length() - 1; i <= j; i++, j--)
{
if(s[i] != s[j])
{
if(s[i] == s[j - 1])
{
int ii = i, jj = j - 1;
for(; ii <= jj && s[ii] == s[jj]; ii++, jj--);
if(ii > jj)
return true;
}
if(s[i+1] == s[j])
{
int ii = i+1, jj = j;
for(; ii <= jj && s[ii] == s[jj]; ii++, jj--);
if(ii > jj)
return true;
}
return false;
}
}
return true;
}
};