给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
输入: "A man, a plan, a canal: Panama"
输出: true
解释:"amanaplanacanalpanama" 是回文串
思路:
1.首先进行去除特殊符号,进行全部进行小写
2.使用双指针方法进行遍历判断
const isPalindrome = (str: string): boolean => {
const str = s.toLocaleLowerCase().replace(/[\W_]/ig, '');
let left = 0, right = str.length - 1;
while (left <= right) {
if (str[left] !== str[right]) {
return false;
}
left += 1;
right -= 1;
}
return true
}










