
Leetcode Problem 125: Valid Palindrome
Given a string s, determine whether it is a valid palindrome after the following normalization:
a–z and digits 0–9).Return true if the cleaned string is a palindrome, or false otherwise. An empty string (after cleaning) is considered a valid palindrome.
function isPalindrome(s: string): boolean {
let stringWithoutSpaces = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()\s@;\"\[\]'\?]/g,"").toLowerCase()
for (let i = 0, k = stringWithoutSpaces.length - 1; i < stringWithoutSpaces.length / 2; i++, k--) {
if (stringWithoutSpaces.charAt(i) !== stringWithoutSpaces.charAt(k)) {
return false
}
}
return true
}
console.log(isPalindrome("A man, a plan, a canal: Panama"))
console.log(isPalindrome("race a car"))
console.log(isPalindrome(" "))