코딩테스트/프로그래머스

[프로그래머스] 괄호 회전하기 - 스택,큐

to,min 2024. 10. 12. 11:52

 

https://school.programmers.co.kr/learn/courses/30/lessons/76502

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

"""
x 만큼 회전 -> 즉 첫번째를 마지막 원소로 보내기
한번씩 회전했을때 올바른 괄호인가 판별하는것

올바른 괄호
닫힘을 스택으로 쌓음
열림이 나왔을떄 스택 pop 이랑 비교해서 맞으면 패스 아니면 out


for x 만큼:
    popleft 후 append 마지막
    for len(deque):
      


"""

from collections import deque

def solution(s):
    s = deque(list(s))
    answer = 0
    for i in range(len(s)):
        close = deque([])
        for j in range(len(s)):
            if s[len(s)-1-j] in ["]",")","}"]:
                close.append(s[len(s)-1-j])
            else:
                if len(close) > 0:
                    a = close.pop()
                else:
                    break
                if s[len(s)-1-j] == "[" and a == "]": # 짝이 맞으면 패스
                    pass
                elif s[len(s)-1-j] == "{" and a == "}":
                    pass
                elif s[len(s)-1-j] == "(" and a == ")":
                    pass
                else: # 안맞으면 틀린겨
                    break
            if j == len(s)-1 and len(close) ==0: # 모든 스택 다 써야함, 즉 남는 괄호 없어야함
                answer += 1

        x = s.popleft()
        s.append(x)   
    return answer