Algorithm/백준

[백준] 괄호(9012) - 파이썬

potato_pizza 2024. 6. 21. 15:03
728x90

괄호(9012)

https://www.acmicpc.net/problem/9012

 

문제

코드

  • 스택을 활용한 괄호 처리 문제
  • '('가 들어오면 쌓고, ')'가 들어오면 pop을 활용해 스택 내의 '('를 뽑아내기
# 괄호
# 9012

import sys
input = sys.stdin.readline

T = int(input())

for _ in range(T):
    stack = []
    paren = input()
    for i in paren:
        if i == '(':
            stack.append(i)
        elif i == ')':
            if stack:
                stack.pop()
            else:
                print('NO')
                break
    else:
        if not stack:
            print('YES')
        else:
            print('NO')
728x90
반응형