https://leetcode.com/problems/valid-palindrome/
유효한 펠린드롬인지 확인하는 문제
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 |
댓글