01. 유효한 팰린드롬(leetcode: 125)
풀이
문제를 보면 considering only alphanumeric and character and ignoring cases
라는 조건이 있다.
따라서 인자로 받은 문자열 s 에 조작을 가해서 영어, 숫자로 만드는 작업이 필요함.
정규표현식
re
모듈을 이용해서 원하지 않는 문자들을 없애자소문자로 만들자
이후에는
deque
를 이용해서 앞과 뒤를 하나씩pop
하면서 조사
# re.sub(pattern, repl, string, count=0, flags=0)
s = re.sub([^A-Za-z0-9], "", s).lower()
deq = collections.deque(s.lower())
while len(deq) > 1:
if deq.popleft() != deq.pop():
return False
return True
Last updated
Was this helpful?