Description
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.)
The string will only contain lowercase characters.
The maximum length of the string is 50000.
Example
Example 1:
Input: s = “aba”
Output: true
Explanation: Originally a palindrome.
Example 2:
Input: s = “abca”
Output: true
Explanation: Delete ‘b’ or ‘c’.
Example 3:
Input: s = “abc”
Output: false
Explanation: Deleting any letter can not make it a palindrome.
Tags
Company
NetEase
Facebook
解法1:
双指针,一前一后往中间靠。当发现不相等时,看去掉p1或p2对应的字符后是否为字符串即可。代码比较罗嗦,还可以简化。
#include <iostream>
#include <string>
using namespace std;
bool validPalindrome(string &s) {
if (s.size()==0) return false;
bool isVal=true;
int p1=0, p2=s.size()-1;
while (p1<p2) {
if (s[p1]!=s[p2]) {
if (p1<p2-1) {
int p3=p1, p4=p2-1;
while(p3<p4) {
if (s[p3]!=s[p4]) isVal=false;
p3++; p4--;
}
if (isVal) return true;
p3=p1+1; p4=p2;
while(p3<p4) {
if (s[p3]!=s[p4]) return false;
p3++; p4--;
}
return true;
} else
return true;
} else {
p1++; p2--;
}
}
return true;
}
int main()
{
string s="eeccccbebaeeabebccceea";
cout<<validPalindrome(s)<<endl;
string s1="aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga";
cout<<validPalindrome(s1)<<endl;
string s2="deeee";
cout<<validPalindrome(s2)<<endl;
return 0;
}
解法2:递归。时间复杂度O(n)。
下面这个解法是网上的,很简单很强大。
bool validPalindrome(string s) {
return valid(s, 0, s.length() - 1, 1);
}
private:
bool valid(string& s, int i, int j, int d) { // d: num of chars you can delete at most
return i >= j || (s[i] == s[j] ? valid(s, i + 1, j - 1, d) : d > 0 && (valid(s, i + 1, j, d - 1) || valid(s, i, j - 1, d - 1)));
}
写成下面这种形式更清楚一点:
class Solution {
public:
/**
* @param s: a string
* @return bool: whether you can make s a palindrome by deleting at most one character
*/
bool validPalindrome(string &s) {
int n = s.size();
if (n <= 1) return true;
return helper(s, 0, n - 1, 1);
}
private:
bool helper(string &s, int a, int b, int d) {
if (a >= b) return true;
if (s[a] == s[b]) {
return helper(s, a + 1, b - 1, d);
} else if (d > 0) {
return helper(s, a + 1, b, d - 1) || helper(s, a, b - 1, d - 1);
}
}
};
解法3:即解法1的简化版本。时间复杂度O(n)。
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;
}
};
4刷:不太简洁。
class Solution {
public:
/**
* @param s: a string
* @return: whether you can make s a palindrome by deleting at most one character
*/
bool validPalindrome(string &s) {
int n = s.size();
int start = 0, end = n - 1;
int diffCount = 0;
while (start < end) {
if (s[start] != s[end]) {
//if (diffCount == 1) return false;
diffCount++;
start++;
} else {
start++;
end--;
}
}
if (diffCount <= 1) return true;
diffCount = 0;
start = 0, end = s.size() - 1;
while (start < end) {
if (s[start] != s[end]) {
//if (diffCount == 1) return false;
diffCount++;
end--;
} else {
start++;
end--;
}
}
if (diffCount <= 1) return true;
return false;
}
};
写成下面这样也可以,稍微简洁一点。
class Solution {
public:
/**
* @param s: a string
* @return: whether you can make s a palindrome by deleting at most one character
*/
bool validPalindrome(string &s) {
int start = 0, end = s.size() - 1;
int diffCount = 0;
while (start < end) {
if (s[start] != s[end]) {
diffCount++;
} else {
end--;
}
start++;
}
if (diffCount <= 1) return true;
diffCount = 0;
start = 0, end = s.size() - 1;
while (start < end) {
if (s[start] != s[end]) {
if (diffCount == 1) return false;
diffCount++;
} else {
start++;
}
end--;
}
//if (diffCount <= 1) return true;
return true;
}
};