LeetCode 125. Valid Palindrome
class Solution:
def isPalindrome(self, s: str) -> bool:
new_s = s.lower()
head = 0
tail = len(s) - 1
while head < tail:
if new_s[head].isalnum() and new_s[tail].isalnum():
if new_s[head] != new_s[tail]:
return False
head += 1
tail -= 1
if not new_s[head].isalnum():
head += 1
if not new_s[tail].isalnum():
tail -= 1
return True
まずs.lower()で大文字を小文字に直す
新しい変数new_sに代入するのがポイント
while文で文字列の前と後ろから一文字ずつ確認していく
英数字であるかどうかはisalnum()で判別できる