코딩테스트/Python
[leetcood] 125. Valid Palindrome
go.od_planter
2024. 10. 11. 16:34
class Solution:
def isPalindrome(self, s: str) -> bool:
# First way
strs = []
for char in s:
if char.isalnum():
strs.append(char.lower())
# Is Palindrome?
while len(strs) > 1:
if strs.pop(0) != strs.pop():
return False
return True
###################################################################################
# Second way
strs: Deque = collections.deque()
for char in s:
if char.isalnum():
strs.append(char.lower())
while len(strs) > 1:
if strs.popleft() != strs.pop():
return False
return True
###################################################################################
# Third way
s = s.lower()
# 정규식으로 불필요한 문자 필터링
s = re.sub('[^a-z0-9]', '', s)
return s == s[::-1]
728x90
반응형