Two pointers: Valid Palindrome II
by Raul Bojalil
HTML
<p>
Write a function that takes a string as input and checks whether it can be a valid palindrome by removing at most one character from it.
</p>
JavaScript
function isPalindrome(s) {
let lp = 0;
let rp = s.length - 1;
while (rp > lp) {
const left = s[lp];
const right = s[rp];
if (left != right) {
const isPalindromeL = isPalindrome2(s.substring(lp + 1, rp + 1));
const isPalindromeR = isPalindrome2(s.substring(lp, rp ));
return isPalindromeL || isPalindromeR;
}
lp++;
rp--;
}
return false;
}
function isPalindrome2 (input) {
console.log(input);
let lp = 0;
let rp = input.length - 1;
while (rp > lp) {
if (input[lp] !== input[rp]) return false;
rp--;
lp++;
}
return true;
}
//console.log(isPalindrome('madame'));
console.log(isPalindrome('tegbaabet'));
//console.log(isPalindrome('abca'));
//console.log(isPalindrome('tebbem'));
//console.log(isPalindrome('eeccccbebaeeabebccceea'));