Untitled
raw download clone
TEXT
views 24
,
size 993 b
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the isBalanced function below.
def isBalanced(s):
    stack = []
    # map right bracket (key) to left bracket (value)
    right = {'}':'{',')':'(',']':'['}
    left = ['(','{','[']
    for c in s:
        # push all left brackets to the stack
        if c in left:
            stack.append(c)
        else:
            if len(stack) > 0 and stack[-1] == right[c]:
                # matches
                stack.pop()
            else:
                # doesn't match
                return 'NO'
    # if anything is still on the stack
    if len(stack) > 0:
        return 'NO'
    # made it through all tests
    return 'YES'
    
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        s = input()

        result = isBalanced(s)

        fptr.write(result + '\n')

    fptr.close()
close fullscreen
Login or Register to edit or fork this paste. It's free.