大家好,我是毛毛。ヾ(´∀ ˋ)ノ
廢話不多說開始今天的解題Day~
Given a string s
, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
1 <= s.length <= 2 * 10^5
s
consists only of printable ASCII characters.首先先簡單的翻譯一下題目
給一個字串,要把除了英文字母以外的符號和空白都刪掉,然後看這個字串是不是對稱的(忽略大小寫)。
作法大致上是這樣
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.strip()
for character in ['\\', '`', '*', '_', '{',\
'}', '[', ']', '(', ')', \
'>', '#', '+', '-', '.',
'!', '$', '\'', ' ', ',',\
':', '%', '^', '<', '&',\
'=', '?', '{', '}', '|',\
'/', '~', '@', '\"', ';']:
if character in s:
s = s.replace(character,"")
s = s.lower()
# print(s)
low = 0
high = len(s)-1
while(low<=high):
if s[low] != s[high]:
return False
low += 1
high -= 1
return True
Python
大家明天見