Avatar


algorithms

Balanced brackets in Python

Apr 10, 2020


You can find the problem here and try to solve it by yourself. Follows my solution and then a concise explanation. def isBalanced(s): """ Checks if s contains balanced brackets. :returns: 'Yes' if s is balanced otherwise 'NO' :rtype: str """ parentheses = { '{': 1, '[': 2, '(': 3, '}': -1, ']': -2, ')': -3 } stack = [] for elem in s: elem = parentheses[elem] if elem > 0: stack. Read Moreā€¦