Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

1
2
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

1
2
Input: "race a car"
Output: false

判斷是不是回文,把字串去掉大小寫跟符號,比對反轉是否相同就可以了。

1
2
3
4
5
6
7
8
9
10
T: O(1), S: O(1)
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower();
s = re.sub('[^a-z0-9]','',s) # clear everything except a-z, 0-9
print(s)
if s == s[::-1]:
return True
else:
return False

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels