[leetcode-python3] 125. Valid Palindrome

shsh·2020년 12월 18일
0

leetcode

목록 보기
30/161

125. Valid Palindrome - python3

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.

My Answer 1: Accepted (Runtime: 44 ms - 76.53% / Memory Usage: 15.7 MB - 19.60%)

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
  1. re 라이브러리를 이용해서 문자열의 공백과 특수문자를 제거함

    << 참고 >>

  2. 대소문자를 구분할테니 upper 함수로 모두 대문자로 변경

  3. 대칭에 위치하는 알파벳끼리 비교 -> 다르면 무조건 false

파이썬은 함수들이 날 살리는 거 같다...^^

profile
Hello, World!

0개의 댓글

관련 채용 정보