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.
Python3solution:defisBalanced(s):# Write your code herelength=len(s)if(length%2!=0):return"NO"open_=["{","(","["]close_=["}",")","]"]stack_open=[]stack_close=[]foriinrange(length):if(s[i]inopen_):stack_open.append(s[i])else:if(stack_open==[]):return"NO"stack_close.append(s[i])if(match(stack_open[-1],stack_close[0])):stack_open.pop()stack_close.pop()if(stack_open==[]andstack_close==[]):return"YES"else:return"NO"defmatch(a,b):if(a=="{"andb=="}"):returnTrueelif(a=="("andb==")"):returnTrueelif(a=="["andb=="]"):returnTrueelse:returnFalse
`
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 →
`