This repository was archived by the owner on Nov 30, 2022. It is now read-only.
Open
Changes from 1 commit
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Next Next commit
balanced brackets using stacks python code.
  • Loading branch information
@pallavisavant
pallavisavant committedOct 3, 2020
commit 7e3dd663278355c7358c3b1dc554774a6133678b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import os

'''
SAMPLE INPUT =
3
{[()]}
{[(])}
{{[[(())]]}}

SAMPLE OUTPUT=
YES
NO
YES


'''
def isBalanced(s):
table = {')': '(', ']': '[', '}': '{'}
stack = []
for x in s:
if stack and table.get(x) == stack[-1]:
stack.pop()
else:
stack.append(x)
if stack:
return "NO"
else:
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()