fork download
  1. import sys
  2. input = sys.stdin.readline
  3.  
  4. T = int(input())
  5. for i in range(T):
  6. L = []
  7. ps = input().strip()
  8. if ('(' not in ps) or (')' not in ps):
  9. print('NO')
  10. elif ps.count('(') != ps.count(')'):
  11. print('NO')
  12. else:
  13. for j in ps:
  14. if j == '(':
  15. L.append('(')
  16. else:
  17. if len(L) != 0 and L[-1] == '(':
  18. del L[-1]
  19. else:
  20. print('NO')
  21. if len(L) == 0:
  22. print('YES')
Success #stdin #stdout 0.03s 9716KB
stdin
1
()))((
stdout
NO
NO