• + 0 comments

    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.
        def isBalanced(self, s: str, stack: deque) -> bool:
            c: str = s[0]
            
            if self.isOpenBracket(c):
                stack.append(c)
            else:
                stack_top: str = stack.pop()
                if not self.areBothBracketsBalanced(stack_top, c):
                    print('\tnot-match: ', stack_top, c)
    
                    return False
                
                if len(s) == 1 and len(stack) == 0:
                    print('\tfinal-case OK')
                    
                    return True
            
            # New solution.
            new_sol: str = s[1:]
            
            if len(new_sol) > 0:
                return self.isBalanced(new_sol, stack)