문제

string이 주어지는데, 알파벳을 모두 소문자로 변경하고 나머지 문자는 제외하였을때 이 문자열이 회문인지를 판단한다.

예시) Example 1:

Input: s = “A man, a plan, a canal: Panama” Output: true Explanation: “amanaplanacanalpanama” is a palindrome. Example 2:

Input: s = “race a car” Output: false Explanation: “raceacar” is not a palindrome. Example 3:

Input: s = “ “ Output: true Explanation: s is an empty string “” after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.

풀이

import re
class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = s.lower()
        s = re.sub(r'[^a-z]',"",s)
        if s == s[::-1]:
            return True
        else: return False

알게된 점