less than 1 minute read

문제링크

스택 활용하여 구현하기

파이썬


def bra(s):
        stack = []
        for i in s:
            if len(stack) == 0: stack.append(i)
            else:
                if i == ")" and stack[-1] == "(":   
                    stack.pop()
                elif i == "]" and stack[-1] == "[":  
                    stack.pop()
                elif i == "}" and stack[-1] == "{":  
                    stack.pop()
                else: stack.append(i)
                
        return 1 if len(stack) == 0 else 0
        
def solution(s):
    answer = 0
    
    for i in range(len(s)):
        if bra(s):  
            answer +=1
        s = s[1:] + s[:1]
    return answer
    

카테고리:

업데이트:

댓글남기기