Stacks: Balanced Brackets

  • + 3 comments

    Python solution using stack principle:

    def is_matched(expression):
        dic = {'{':'}','[':']','(':')'}
        lst =[]
        for i in expression:
            if i in dic.keys():
                lst.append(dic[i])
            elif len(lst)>0 and i==lst[-1]:
                lst.pop()
            else: 
                return False
        return len(lst) == 0