Given a string s, check if it is possible to make it a palindrome by deleting AT MOST one character (meaning zero deletions is acceptable). String s will contain <50,000 lowercase alphabetical characters.
The code I wrote below passed 458/460 test cases, and it got stuck on one in particular with no obvious reason, returning false instead of true. The logic of the algorithm is simple, and I've tried moving conditionals around but nothing seems to change.
class Solution {
public:
bool ispalindrome; //holds result
bool validPalindrome(string s) {
bool candelete = true; //allows one delete
ispalindrome = true; //initial condition
int lcursor = 0;
int rcursor = s.length() - 1;
while(lcursor < rcursor && ispalindrome){
//if cursor points at different letters
if(s[lcursor] != s[rcursor]){
// if delete is still allowed and delete works
if(s[lcursor + 1] == s[rcursor] && candelete){
lcursor++;
candelete = false;
} else if (s[lcursor] == s[rcursor - 1] && candelete){
rcursor--;
candelete = false;
} else {
ispalindrome = false;
}
}
lcursor++;
rcursor--;
}
return ispalindrome;
}
};
The test case that trips this solution is as follows:
aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga
Code testing with this testcase:
#include <iostream>
using std::string;
// class Solution { ... etc., from above
int main() {
string s = "aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga";
std::cout << Solution().validPalindrome(s) << std::endl;
};