본문 바로가기

Algorithm/Leetcode

[Top Interview 150 - Two Pointers] 125. Valid Palindrome

[Q] [Easy]

A phrase is a palindrome if, 
after converting all uppercase letters into lowercase letters and 
removing all non-alphanumeric characters, 
it reads the same forward and backward. 
Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

 

  • palindrome 이 뭔지 몰라서 처음부터 띠용했다.
  • 리스트 안의 문자열을 뒤집는 함수 [::-1] 만 알면 쉬운 문제! 까먹고 있어서 어렵게 돌아가다가 찾았다.

 

# Answer

import re

class Solution:
    def isPalindrome(self, s: str) -> bool:
        
        s = s.lower()
        s = re.sub('[^a-z0-9]', '', s)

        return s == s[::-1]