# [LeetCode] 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.

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;
}
};

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;
}
};

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

，如需转载请自行联系原博主。

|
8月前
|
Python
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
31 0
|
8月前
LeetCode 409. Longest Palindrome

43 0
|
8月前
|

LeetCode 336. Palindrome Pairs

60 0
|
8月前
|

LeetCode 214. Shortest Palindrome

31 0
|
8月前
|
canal
LeetCode 125. Valid Palindrome

47 0
【LeetCode】Palindrome Pairs（336）
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a 　　palindrome.
58 0
LeetCode之Palindrome Number（回文数）
LeetCode之Palindrome Number（回文数）
47 0
|
Java Python