관리 메뉴

솜씨좋은장씨

[leetCode] 125. Valid Palindrome (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 125. Valid Palindrome (Python)

솜씨좋은장씨 2020. 6. 16. 18:11
728x90
반응형

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.

 

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false

Solution

import re

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

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments