NaNa

NaNa

Valid Palindrome

· 3 min read

Valid Palindrome - LeetCode

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true*if it is apalindrome, orfalseotherwise*.

Array Reverse

var isPalindrome = function (s) {
  s = s.replaceAll(/[^a-z0-9]/gi, '').toLowerCase();
  return s.split('').reverse().join('') === s;
};

Two Pointers

var isPalindrome = function (s) {
  s = s.replaceAll(/[^a-z0-9]/gi, '').toLowerCase();
  let l = 0;
  let r = s.length - 1;
  while (l < r) {
    if (s[l] !== s[r]) return false;
    l++;
    r--;
  }
  return true;
};

regex, algorithm/two_pointers, algorithm/string