[프로그래머스] 월간 코드 챌린지2 - 괄호 회전하기

Juhyang·2021년 4월 21일
0

알고리즘 풀이

목록 보기
15/16
post-thumbnail
def solution(s):
    answer = 0
    temp = list(s)
    
    for i in range(len(s)) :
        # 한칸씩 밀어주기
        a = temp[0]
        temp = temp[1:]
        temp.append(a)
        
        stack = []
        
        for j in temp:
            if len(stack) == 0:
                stack.append(j)
                continue;
                
            # 열리는 괄호
            if (stack[-1] == '[' or
                stack[-1] == '{' or
                stack[-1] == '(') :
               
                stack.append(j)
    
              
            # 스택길이 1개인데 바로 닫히는 괄호
            if (len(stack) == 1 and 
                  stack[0] == ']' or
                  stack[0] == '}' or
                  stack[0] == ')'
                 ) :
                
                break;
            
            # 스택길이 2개 이상인데 닫힐경우 매칭되는지 검사
            if  ((stack[-1] == ']' and stack[-2] == '[') or
                 (stack[-1] == '}' and stack[-2] == '{') or
                 (stack[-1] == ')' and stack[-2] == '(')) :

                stack.pop()
                stack.pop()

	# 모두 매칭되어 스택이 비어있을 경우
        if len(stack) == 0:
            answer += 1
                

    return answer

아 ㅜㅜ
다시 차분히 푸니까 금방 풀리는데
테스트 시작하면 너무 떨려서 그런지 엄청 어렵게 풀게된다..

profile
kurly - commerce web development

0개의 댓글