βConvert to Palindrome
bool isPalindrome(const string &str, int i, int j) {
while(i < j)
if(str[i++] != str[j--])
return false;
return true;
}
int Solution::solve(string A) {
int i = 0, j = A.size() - 1;
while(i < j)
if(A[i] == A[j])
i++, j--;
else
return isPalindrome(A, i + 1, j) || isPalindrome(A, i, j - 1);
return (A.size() & 1);
}
Time Complexity: β
Space Complexity: β
The given solution on the website is wrong.
They are returning true after checking if no mismatch is found i.e. the given string is a valid palindrome.
But they are missing the fact that a valid palindrome cannot be converted to a valid palindrome by removing a single character if the palindrome is of even length.
Last updated