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 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Code
# Python, no Regex build indefisPalindrome(self,s:str) ->bool: l, r =0,len(s)-1while l < r:while l < r andnot s[l].isalnum(): l +=1while l <r andnot s[r].isalnum(): r -=1if s[l].lower()!= s[r].lower():returnFalse l +=1; r -=1returnTrue
// Javascript, use Regex filter out all lettersvarisPalindrome=function(s) { s =s.toLowerCase().replace(/[\W_]/g,'');for(let l=0,r=s.length-1; l<=r; ++l,--r){if(s[l] != s[r]){returnfalse; } }returntrue;};