[String] 9012번 - 괄호(29일차)

bob.sort·2021년 6월 13일
1
post-thumbnail
# 코드 실행 시간 단축
import sys
input = sys.stdin.readline
# input 정의
n = int(input())
# 스택 선언 (실행시간 단축을 위해 int 변수로 선언)
stack = 0
# n번 동안
for i in range(n):
    # 문자열을 입력받고 (readline 개행문자 rstrip으로 제거)
    string = input().rstrip()
    # while문 상 문자열 index를 위한 int 변수
    index = 0
    # 저장된 '('의 개수가 존재한다면, 문자열의 길이 만큼 반복)
    while(stack >= 0 and index < len(string)):
        # '('일 경우 stack에 1을 추가
        if(string[index] == '('):
            stack += 1
        # ')'일 경우 stack에 1을 뺌
        else:
            stack -= 1
        # 문자열 인덱스 이동
        index += 1
    # 저장된 '('와 ')'의 개수가 같고 순서가 올바르다면 YES 출력
    if(stack == 0):
        print("YES")
    # 그렇지 않다면 No 출력
    else:
        print("NO")
    # stack 초기화
    stack = 0
profile
Interest in Computer Graphics and Computer Vision

0개의 댓글