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.
双指针,92ms
class Solution {
public:
bool fun(string s) {
int len = s.size();
int i = 0, j = len - 1;
while (i <= j) {
if (s[i++] != s[j--]) {
return false;
}
}
return true;
}
bool validPalindrome(string s) {
int len = s.size();
int i = 0, j = len - 1;
while (i <= j) {
//cout << s[i] << s[j] << endl;
if (s[i] != s[j]) {
++i;
if (fun(s.substr(i, j - i + 1)) == true) {
return true;
}
--i;
--j;
if (fun(s.substr(i, j - i + 1)) == true) {
return true;
}
return false;
}
else {
++i;
--j;
}
}
return true;
}
};