Understanding the trick


#1

Almost Similar to normal isPalindrome function.
Trick : Inside the while loop if A[l] !=A[r] then we need to check if skipping the left character would result in a Palindrome or skipping the right character would do it. if both doesn’t help : return false;

bool isPalindrome(string A,int l,int r){
if(l>=r)return true;
while(l<=r){
if(A[l]==A[r]){
l++;
r–;
}
else return false;
}
return true;
}
int Solution::solve(string A) {
int l=0;
int n=A.size();
if(n<=1)return true;
int r=n-1;
while(l<=r){
if(A[l]==A[r]){
l++;
r–;
}
else{
return isPalindrome(A,l+1,r)||isPalindrome(A,l,r-1);
}
}
return true;
}