125. Valid Palindrome
Problem
https://leetcode.com/problems/valid-palindrome/
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
Solution
Just burn the candle at both ends.
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
if (typeof s !== 'string') { return false }
// empty stiring
if (!s) { return true }
let ss = s.toLowerCase()
let i = 0
let j = ss.length - 1
let alphanumChecker = /\w/
while (i < j) {
while (!alphanumChecker.test(ss[i])) {
if (i >= j) { return true }
i += 1
}
while (!alphanumChecker.test(ss[j])) {
if (i >= j) { return true }
j -= 1
}
if (ss[i++] !== ss[j--]) { return false }
}
return true
};