We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is my python solution. It's consise and self explanatory if you understand the application of a stack.
defisBalanced(s):stack=[]open_to_close={"{":"}","[":"]","(":")"}forbracketins:# Open bracket caseclosing_bracket=open_to_close.get(bracket)ifclosing_bracket:stack.append(closing_bracket)# Close bracket caseelifnotstackorbracket!=stack.pop():return"NO"# If we have looped through the entire string and the stack is empty, all opening brackets were closedreturn"NO"ifstackelse"YES"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
This is my python solution. It's consise and self explanatory if you understand the application of a stack.