Skip to content

Instantly share code, notes, and snippets.

@burkeholland
Created November 29, 2022 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burkeholland/03bc7a0c07ccf6be3c845e3df3851ea6 to your computer and use it in GitHub Desktop.
Save burkeholland/03bc7a0c07ccf6be3c845e3df3851ea6 to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
// two pointer method
left = 0;
right = s.length - 1;
sl = s.toLowerCase();
while (left < right) {
ralpha = isAlphaNumeric(s[right]);
lalpha = isAlphaNumeric(s[left]);
// it's not a palindrome
if (ralpha && lalpha && sl[right] != sl[left]) return false;
// the letters/numbers match
if (ralpha && lalpha && sl[right] === sl[left]) {
right--;
left++;
} else {
// otherwise one of these is a special character so move the pointer and try again
if (!ralpha) right--;
if (!lalpha) left++;
}
}
return true;
};
function isAlphaNumeric(c) {
// check to make sure it's not a special character
if (c.toUpperCase() != c.toLowerCase()) return true;
// check to see if it's a number
return !isNaN(parseFloat(c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment