모든 로테이션을 돌려서 직접 확인해봐도 시간초과가 나지 않는다.
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
'Algorithem > 프로그래머스 PS with code' 카테고리의 다른 글
[python] [3차] 파일명 정렬 lv.2 (0) | 2023.06.24 |
---|---|
[python3] 퍼즐 조각 채우기 lv.3 : 구현 (0) | 2023.06.24 |
[python3] H-index lv.2 (0) | 2023.01.26 |
[python3] 멀리 뛰기 lv.2 (0) | 2023.01.26 |
[python3] 점프와 순간이동 lv.2 (0) | 2023.01.26 |