LEETCODE - Palindrome

Coaspe·2021년 5월 3일
0
from collections import deque as Deque
import collections
import re

def isPalindrome(self, s : str) -> bool :
    strs = []
    for char in s:
        if char.isalnum() :
            strs.append(char.lower())


    while len(strs) > 1:
        if strs.pop(0) != strs.pop():
            return False

    return True

def isPalindrome2(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
# pop()은 O(n)이지만 popleft()는 O(1)이다.

def isPalindrome3(self, s: str) -> bool :
    s = s.lower()
    s = re.sub('[^a-z0-9]','',s)
    return s == s[::-1]
#s[::-1] 거꾸로 뒤집기

#파이썬 문자열 슬라이싱을 기준으로 한 파이썬 문자열 처리 실행 시간
# 슬라이싱 -> 리스트 reverse() -> reversed() + join() -> for 반복 -> while 반복 -> 재귀
# 01234
# 안녕하세요
# -5-4-3-2-1
# [::2] 2칸씩 앞으로 이동, [-3:] 뒤에서 3번째부터 끝까지
profile
https://github.com/Coaspe

0개의 댓글