HackerRank 'Balanced Parentheses' Solution

Martin Kysel · January 25, 2016

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:

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()

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.