알고리즘

<백준> 9012번 파이썬 알고리즘

changha. 2021. 7. 10. 14:59
n = int(input())
for _ in range(n):
  m = input()
  l = []
  for i in m:
    if i == "(":
      l.append("(")
    else:
      if len(l) != 0 and l[-1] == "(":
        l.pop()
      else:
        l.append(")")
  if len(l) == 0:
    print("YES")
  else:
    print("NO")

저번에 푼 4949번과 똑같은 알고리즘이다

(일때는 append

)일때는 조건을 만족하면 pop하고 아니면 append한다