“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

[B] [C] [D]

Given a string s, verify that s is a palindrome string. Only alphanumeric characters are considered.

In this case, the empty string is defined as a valid palindrome string.

Example 1:

Input: S = "A man, A plan, A canal: Panama" Output: true "amanaplanacanalpanama" is A palindrome stringCopy the code

Example 2:

Input: s = "raceacar" Output: false Explanation: "raceacar" is not a palindromeCopy the code

In this case, we maintain two variables, which are the beginning and end Pointers respectively. If the matched characters are not letters or digits, skip

So when a pointer points to a letter or number, we lower the characters and compare them, and return false if they are different

Until the heads and tails meet

At this point, our solution is complete, the following is the code implementation

var isPalindrome = function(s) { if(s.length===1) return true; const reg = /[a-zA-Z0-9]/ function check(l,r){ while(l<r){ while(! reg.test(s[l]) && l<r) l++ while(! reg.test(s[r]) && l<r) r-- if(s[l].toLocaleLowerCase() ! == s[r].toLocaleLowerCase()) return false; else l++,r--; } return true; } return check(0,s.length-1) };Copy the code

If you have any questions or suggestions, please leave a comment!