프로그래머스 연습문제 - 괄호 회전하기 (level2)

j_wisdom_h·2022년 12월 6일
0

CodingTest

목록 보기
24/58
post-thumbnail

My Solution

from collections import deque

def solution(s):
    answer = 0
    orign = deque(s)

    for i in range(len(s)): 
        orign.rotate(1) 
        popList =[]
        start, end = orign[0], orign[-1]
        if start == "]" or start == ")" or start == "}":
            continue
        elif end == "[" or end == "(" or end == "{":
            continue
        else:
            popList.append(start)

            for j in list(orign)[1:]:
                if j == "[" or j == "(" or j == "{":
                    popList.append(j)
                else : 
                  try:
                    p = popList[-1]

                    if p == "{"  and j == "}"  or (p == "("  and j == ")") or (p == "["  and j == "]"):  
                          popList.pop()
                          continue
                    else:
                        popList.append('null')
                        break
                  except IndexError:
                        popList.append('null')
                        break
            if popList ==[]:
                answer +=1        



    return answer  

다른 솔루션

def is_valid(s):
    stack = []
    for ch in s:
        if not stack:
            stack.append(ch)
        elif stack[-1] == '(':
            if ch==')': stack.pop()
            else: stack.append(ch)
        elif stack[-1] == '{':
            if ch=='}': stack.pop()
            else: stack.append(ch)
        elif stack[-1] == '[':
            if ch==']': stack.pop()
            else: stack.append(ch)

    return False if stack else True

def solution(s):
    answer = 0
    for i in range(len(s)):
        answer += is_valid(s[i:]+s[:i])
    return answer
profile
뚜잇뚜잇 FE개발자

0개의 댓글