'''
http://www.ardendertat.com/2011/11/08/programming-interview-questions-14-check-balanced-parentheses/
'''




def isBalanced(expr):
    if len(expr)%2!=0:
        return False
    opening=set('([{')
    match=set([ ('(',')'), ('[',']'), ('{','}') ])
    stack=[]
    for char in expr:
        if char in opening:
            stack.append(char)
        else:
            if len(stack)==0:
                return False
            lastOpen=stack.pop()
            if (lastOpen, char) not in match:
                return False
    return len(stack)==0


print(isBalanced('((()))'))
print(isBalanced('((())'))



# def parChecker(symbolString):
#     s = Stack()
#     balanced = True
#     index = 0
#     while index < len(symbolString) and balanced:
#         symbol = symbolString[index]
#         if symbol == "(":
#             s.push(symbol)
#         else:
#             if s.isEmpty():
#                 balanced = False
#             else:
#                 s.pop()
#
#         index = index + 1
#
#     if balanced and s.isEmpty():
#         return True
#     else:
#         return False
#
# parChecker('((()))')
#
# if __name__ == '__main__':
#     print(parChecker('((()))'))
#     print(parChecker('(()'))

