[백준] 9012번 - 괄호

fooooif·2021년 7월 9일
post-thumbnail

✍ 문제

문제링크: https://www.acmicpc.net/problem/9012

👏 풀이과정

문자열 문제이다. "(" 와 ")" 개수를 +1, -1 로 계산해주고 음수가 되면 "NO"를 출력하게 만들었다.

import sys
def find_answer(st):
    cnt = 0
    for s in st:
        if s == "(":
            cnt += 1
        if s == ")":
            cnt -= 1
        if cnt < 0:
            print("NO")
            return
    if cnt == 0:
        print("YES")
    else:
        print("NO")
T = int(sys.stdin.readline())
for _ in range(T):
    st = sys.stdin.readline().strip()
    find_answer(st)
profile
열심히 하자

0개의 댓글