-
Notifications
You must be signed in to change notification settings - Fork 2
/
Balanced Brackets.py
51 lines (39 loc) · 1023 Bytes
/
Balanced Brackets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
Problem Statement: https://www.hackerrank.com/challenges/balanced-brackets/problem
@Coded by TSG, 2020
'''
open_list = ["[","{","("]
close_list = ["]","}",")"]
import math
import os
import random
import re
import sys
open_list = ["[","{","("]
close_list = ["]","}",")"]
# Driver Function
def isBalanced(s):
global open_list
global close_list
stack = []
for i in (s):
if i in (open_list):
stack.append(i)
elif i in (close_list):
pos = close_list.index(i)
if ((len(stack) > 0) and (stack[len(stack)-1]) == open_list[pos]):
stack.pop()
else:
return ("NO")
if len(stack) == 0:
return ("YES")
else:
return ("NO")
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()