전공/백준
9012
yha97
2022. 10. 17. 13:56
날짜 : 2022. 10. 17
사용 언어 : python
문제
코드
import sys
def check(n):
ps = {"(" : 0, ")" : 0}
for i in range(len(n)):
if n[i] == "(":
ps["("] += 1
else:
if ps["("] <= ps[")"]:
return False
ps[")"] += 1
if ps["("] == ps[")"]:
return True
t = int(sys.stdin.readline())
for _ in range(t):
s = sys.stdin.readline().strip()
if check(s):
print("YES")
else:
print("NO")
알게된 점
- stack 문제중 하나였다.
- "(" 의 경우 자유롭게 삽입 가능하지만 괄호를 닫는 ")"는 여는 문자의 개수보다 작아야 하고, 마지막에는 서로 개수가 같아야 한다는 것이 조건이었다.
참고 사이트
-