Algorithem/프로그래머스 PS with code
[python3] 괄호 회전하기 lv.2
jamong5
2023. 1. 26. 18:02
모든 로테이션을 돌려서 직접 확인해봐도 시간초과가 나지 않는다.
N = 1000, O($N^2$)
풀이 1) 스택
괄호문자열인지 확인하기 위해서는 스택으로 풀면 된다.
from collections import deque
def solution(s):
d = deque(s)
cnt = 0
for i in range(len(s)) :
stack = []
for c in d :
if not stack :
stack.append(c)
continue
if c == ')' and stack[-1] == '(' :
stack.pop()
elif c == '}' and stack[-1] == '{' :
stack.pop()
elif c == ']' and stack[-1] == '[' :
stack.pop()
else : stack.append(c)
if not stack : cnt+=1
d.rotate(1)
return cnt