728x90

나의 풀이
from collections import deque
def check(s):
stack = []
for x in s:
if x == '(' or x == '[' or x == '{':
stack.append(x)
elif x == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
return False
elif x == ']':
if stack and stack[-1] == '[':
stack.pop()
else:
return False
elif x == '}':
if stack and stack[-1] == '{':
stack.pop()
else:
return False
if stack:
return False
else:
return True
def solution(s):
answer = 0
s = deque(s)
for _ in range(len(s)):
s.rotate(-1)
if check(s):
answer += 1
return answer
올바른 괄호 문자열을 제거하는 풀이
from collections import deque
def check(s):
while True:
if "()" in s: s=s.replace("()","")
elif "{}" in s: s=s.replace("{}","")
elif "[]" in s: s=s.replace("[]","")
else: return False if s else True
def solution(s):
ans = 0
que = deque(s)
for i in range(len(s)):
if check(''.join(que)): ans+=1
que.rotate(-1)
return ans반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] 2 x n 타일링 (0) | 2022.06.16 |
|---|---|
| [프로그래머스/파이썬] 배달 (0) | 2022.06.16 |
| [프로그래머스/파이썬] 후보키 (0) | 2022.06.16 |
| [프로그래머스/파이썬] 순위 검색 (0) | 2022.06.15 |
| [프로그래머스/파이썬] 예상 대진표 (0) | 2022.06.13 |