728x90

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

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

 

import sys

input = sys.stdin.readline

n = int(input())
g = []
for i in range(n):
    g.append(input().strip())

def VPS(strings):
    stack = []
    for i in range(len(strings)):
        if strings[i] == "(":
            stack.append(strings[i])
        elif strings[i] ==")" and len(stack)!=0:
            if stack[-1] == "(":
                stack.pop()
            else:
                stack.append(strings[i])
        elif strings[i] ==")" and len(stack) ==0:
            stack.append(strings[i])

    return "YES" if len(stack) == 0 else "NO"

for i in range(len(g)):
    print(VPS(g[i]))
728x90

'Algorithm > 백준' 카테고리의 다른 글

[백준] 18258 큐2  (0) 2023.07.06
[백준] 10845 큐  (0) 2023.07.06
[백준] 10828 스택  (0) 2023.07.05
[백준] 10973 이전순열  (0) 2023.03.17
[백준] 10972 다음순열  (0) 2023.03.17

+ Recent posts