题目描述:
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note: The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
class Solution {
public:
bool validPalindrome(string s) {
int i = 0;
int j = s.size() - 1;
while(i <= j)
{
if(s[i] == s[j])
{
i++;
j--;
}
else
return isPalindrome(s.substr(0, i) + s.substr(i + 1)) || isPalindrome(s.substr(0, j) + s.substr(j + 1));
}
return true;
}
bool isPalindrome(string s)
{
int i = 0;
int j = s.size() - 1;
while(i <= j)
{
if(s[i] == s[j])
{
i++;
j--;
}
else return false;
}
return true;
}
};