# HackerRank ‘Balanced Parentheses’ Solution

H
##### Short Problem Definition:

Given a sequence consisting of parentheses, determine whether the expression is balanced.

Balanced Parentheses

##### Complexity:

time complexity is O(N)

space complexity is O(N)

##### Execution:

Equivalent to Codility Brackets.

##### Solution:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 def isValidPair(left, right):     if left == '(' and right == ')':         return True     if left == '[' and right == ']':         return True     if left == '{' and right == '}':         return True       return False    def isProperlyNested(S):     stack = []            for symbol in S:         if symbol == '[' or symbol == '{' or symbol == '(':             stack.append(symbol)         else:             if len(stack) == 0:                 return False             last = stack.pop()             if not isValidPair(last, symbol):                 return False            if len(stack) != 0:         return False                    return True   def main():     N = int(raw_input())       for _ in xrange(N):         s = raw_input()         if isProperlyNested(s):             print "YES"         else:             print "NO"     if __name__ == '__main__':     main()