본문 바로가기
Programming/LeetCode

[LeetCode] Valid Palindrome

by 데이터현 2022. 2. 25.

https://leetcode.com/problems/valid-palindrome/

 

Valid Palindrome - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

유효한 펠린드롬인지 확인하는 문제

Deque를 활용해서 풀이 가능

class Solution:
    def isPalindrome(self, s: str) -> bool:
        strs: Deque = collections.deque()
        for char in s:
            if char.isalnum():
                strs.append(char.lower())
        while len(strs) > 1:
            if strs.popleft() != strs.pop():
                return False
        return True

정규표현식 활용

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

'Programming > LeetCode' 카테고리의 다른 글

[LeetCode] Broken Calculator  (0) 2022.03.23
[LeetCode] Merge Two Sorted Lists  (0) 2022.03.23
[LeetCode] Palindrome Linked List  (0) 2022.03.23
[LeetCode] Reverse String  (0) 2022.02.28
[LeetCode] Two Sum  (0) 2022.02.09

댓글