思路:和判断回文字符串差不多,双指针法
#include <string>
using namespace std;
class Solution
{
public:
bool validPalindrome(string s)
{
return verify(s, 0, s.length() - 1, 0);
}
bool verify(string s, int i, int j, int diffcnt)
{
while (i < j)
{
if (s[i] == s[j])
{
i++;
j--;
}
else
{
diffcnt++;
if (diffcnt > 1)
{
return false;
}
return verify(s, i + 1, j, diffcnt) || verify(s, i, j - 1, diffcnt);
}
}
return true;
}
};
int main(){
Solution solution;
string str;
cin>>str;
if(solution.validPalindrome(str)){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
return 0;
}
-[1]???offerⅡ019.最多删除一个字符得到回文题解
PO91回文字符串