Difficult:Easy
題目
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
翻譯
一個短語是回文,如果在將所有大寫字母轉換為小寫字母並刪除所有非字母數字字符後,它向前和向後讀取相同。 字母數字字符包括字母和數字。
給定一個字符串 s,如果它是回文則返回 true,否則返回 false。
範例
Example 1:
1 | Input: s = "A man, a plan, a canal: Panama" |
Example 2:
1 | Input: s = "race a car" |
Example 3:
1 | Input: s = " " |
解題思路
1.先將非數字及英文字母的符號去掉
2.判斷是否為回文
Solution
1 | let str=s.replace(/[^a-z|^A-Z|^\d]/g,"").toLocaleLowerCase(); |