[Boj] 백준 괄호 9012(python)
문제링크 : https://www.acmicpc.net/problem/9012
괄호로 이루어진 문자열이 주어지고, 괄호의 쌍이 올바른지 검사하는 문제
조건은 크게 두 가지로, 마지막에 (와 )의 갯수가 같은지를 검사하는 것과
중간에 )의 갯수가 많은지 검사하는 것
따라서 stack을 통해 하나씩 빼내면서 검사
import sys
def checkVPS(string):
checker = 0
while len(string):
word = string.pop()
if word == '(':
checker += 1
else:
checker -= 1
if checker < 0:
return 'NO'
if checker == 0:
return 'YES'
else:
return 'NO'
N = int(input())
for _ in range(N):
string = list(sys.stdin.readline().rstrip())
print(checkVPS(string[::-1])) # 첫 번째 원소부터 빼내야 하므로
댓글남기기