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.
import re
class Solution:
def isPalindrome(self, s: str) -> bool:
s = re.sub('[^0-9a-zA-Z]', '', s) # 공백, 특수문자 제거
s = s.upper()
for i in range(0, len(s)//2):
if s[i] != s[len(s)-i-1]:
return False
return True
re 라이브러리를 이용해서 문자열의 공백과 특수문자를 제거함
<< 참고 >>
- 파이썬 특수문자 가려내기 - https://kldp.org/node/53096
- 아스키의 특수문자들 = !"#$%'()*+,-./:;<=>?@[]^_`{|}~
대소문자를 구분할테니 upper 함수로 모두 대문자로 변경
대칭에 위치하는 알파벳끼리 비교 -> 다르면 무조건 false
파이썬은 함수들이 날 살리는 거 같다...^^