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.
I try a recursive solution just for fun (won't pass the test cases)
# rest of the code ...# the methods are naive methods and self explained.# `stack` variable could be considered as a global variable.defisBalanced(self,s:str,stack:deque)->bool:c:str=s[0]ifself.isOpenBracket(c):stack.append(c)else:stack_top:str=stack.pop()ifnotself.areBothBracketsBalanced(stack_top,c):print('\tnot-match:',stack_top,c)returnFalseiflen(s)==1andlen(stack)==0:print('\tfinal-caseOK')returnTrue# New solution.new_sol:str=s[1:]iflen(new_sol)>0:returnself.isBalanced(new_sol,stack)
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 →
I try a recursive solution just for fun (won't pass the test cases)