[백준] 하루5문제(25.01.30)

HAHAHELLO·2025년 1월 30일

파이썬

목록 보기
13/50

스택, 큐, 덱

정렬

괄호 : 9012

문제

예제

나의 풀이

처음엔 "(" = -1, ")" = 1 로 정의하고 sum() = 0으로 풀으려고 했는데 순서가 뒤바뀌더라도 나오는 횟수가 같으면 결국은 0이 되므로 잘못된 생각이었다.

import sys
input = sys.stdin.readline
t = int(input())

for _ in range(t):
    test = input().strip()
    stack = []
    is_vps = True
    for i in test:
        if i == '(':
            stack.append(i)
        else:
            if stack:
                stack.pop()
            else:
                is_vps = False
                
    if is_vps and not stack:
        print('YES')
    else:
        print('NO')
profile
데이터 엔지니어가 되어 봅시다 🌈

0개의 댓글