Question:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example:
Input: "A man, a plan, a canal: Panama"
Output: true
Input: "race a car"
Output: false
Solution:
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
if(!s)return true;
const regex = /[a-zA-Z0-9]+?/gi;
let str = s.match(regex);
if(!str) return true;
let len = str.length;
if(len === 1) return true;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (str[i].toLowerCase() === str[len-i-1].toLowerCase()) continue;
return false;
}
return true;
};
Runtime: 80 ms, faster than 39.01% of JavaScript online submissions for Valid Palindrome.