[LeetCode][JavaScript]Valid Palindrome

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.

https://leetcode.com/problems/valid-palindrome/


双指针,一个指向头,一个指向尾。如果不是字母数字,就继续找,head++, tail--。

 1 /**
 2  * @param {string} s
 3  * @return {boolean}
 4  */
 5 var isPalindrome = function(s) {
 6     s = s.trim();
 7     if(s !== ""){
 8         for(var i = 0, j = s.length - 1; i <= j; i++, j--){
 9             while(!/[a-z0-9]/i.test(s[i]) && i < s.length && i < j){
10                 i++;
11             }
12             while(!/[a-z0-9]/i.test(s[j]) && j > 0 && i < j){
13                 j--;
14             }
15             if(s[i].toUpperCase() !== s[j].toUpperCase()){
16                 return false;
17             }
18         }
19     }
20     return true;
21 };