본문 바로가기
알고리즘/소스코드

PROGRAMMERS 76502: 괄호 회전하기

by cjw.git 2021. 6. 21.

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


Python

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from collections import deque
 
 
def solution(s):
    deq = deque(s[:])
    result = 0
    length = len(s)
    for i in range(length):
        temp = deq.popleft()
        deq.append(temp)
        stack = []
        for i in deq:
            if len(stack) == 0:
                if i in [')'']''}']:
                    break
                stack.append(i)
            else:
                if i in ['(''[''{'')'']''}']:
                    if stack[-1== '(' and i == ')':
                        stack.pop()
                    elif stack[-1== '[' and i == ']':
                        stack.pop()
                    elif stack[-1== '{' and i == '}':
                        stack.pop()
                    else:
                        stack.append(i)
 
        else:
            if len(stack) == 0:
                result += 1
    return result
cs

FeedBack

  1.  

 

 

 

cjw.git@gmail.com

댓글