1. 문제
https://leetcode.com/problems/valid-palindrome/
2. 접근 방법
주어진 문자열이 팰린드롬인지 확인하면 됩니다.
문자열을 확인하는 방법은 여러 가지가 있는ㄷㅔ용
1. 리스트
2. 데크
저는 deque를 사용했슴다
왜냐면 리스트는 pop(0)이 O(n)이지만
데크는 popleft()가 O(1)이니까여
3. 코드
python3
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
4. 마치며
사실 더 시간을 줄이기 위해서는 정규식을 사용해도 되는데여
아직 손에 안익어서 다음에 시도해보겠습니다요
'Algorithm > Python' 카테고리의 다른 글
[LeetCode] 937. Reorder Log Files (2) | 2021.06.02 |
---|---|
[LeetCode] 344. Reverse String (0) | 2021.06.01 |
[백준 1759] 암호만들기 (1) | 2021.05.27 |
[백준 11723] 집합 (1) | 2021.05.26 |
[백준 10819] 차이를 최대로 (0) | 2021.05.25 |