# 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:

``````**Input:** "aba"
**Output:** True
``````

Example 2:

``````**Input:** "abca"
**Output:** True
**Explanation:** You could delete the character 'c'.
``````

Note:

``````1. 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 left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] != s[right]) return isValid(s, left, right - 1) || isValid(s, left + 1, right);
++left; --right;
}
return true;
}
bool isValid(string s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) return false;
++left; --right;
}
return true;
}
};
``````

``````class Solution {
public:
bool validPalindrome(string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] == s[right]) {
++left; --right;
} else {
int l = left, r = right - 1;
while (l < r) {
if (s[l] != s[r]) break;
++l; --r;
if (l >= r) return true;
}
++left;
while (left < right) {
if (s[left] != s[right]) return false;
++left; --right;
}
}
}
return true;
}
};
``````

Valid Palindrome

https://discuss.leetcode.com/topic/103939/java-o-n-time-o-1-space

https://discuss.leetcode.com/topic/103911/two-solutions-optimized-and-recursive-java-and-c

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation