Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- class Solution:
- #Function to check if brackets are balanced or not.
- def ispar(self,x):
- # code here
- startChars = ['{', '[', '(']
- metStartingChars = []
- charMap = {
- "{":"}","[":"]","(":")"
- }
- if (len(x) % 2 != 0 ) or (x[0] not in startChars) :
- return False
- for char in x:
- # if char is a starting character lets expect an ending for it
- if char in startChars:
- metStartingChars.append(char)
- # else if ending character? it be the ending character iof the last starting character we met
- else:
- lastestStart = metStartingChars[-1] if 0 < len(metStartingChars) else None
- if charMap.get(lastestStart, None) == char :
- metStartingChars.pop()
- else:
- return False
- return not metStartingChars
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- import atexit
- import io
- import sys
- #Contributed by : Nagendra Jha
- _INPUT_LINES = sys.stdin.read().splitlines()
- input = iter(_INPUT_LINES).__next__
- _OUTPUT_BUFFER = io.StringIO()
- sys.stdout = _OUTPUT_BUFFER
- @atexit.register
- def write():
- sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
- if __name__ == '__main__':
- test_cases = int(input())
- for cases in range(test_cases) :
- #n = int(input())
- #n,k = map(int,imput().strip().split())
- #a = list(map(int,input().strip().split()))
- s = str(input())
- obj = Solution()
- if obj.ispar(s):
- print("balanced")
- else:
- print("not balanced")
- # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement