Wednesday, June 11, 2014

LeetCode (Python): Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Solution:

class Solution:
    # @return a boolean
    def isValid(self, s):
        stack = []
        for i in range(0, len(s)):
            if s[i] == '(' or s[i] == '[' or s[i] == '{':
                stack.append(s[i])
            else:
                if len(stack)==0:
                    return False
                lastOpenParenthesis = stack.pop()
                if (s[i]==')' and lastOpenParenthesis !='(') or (s[i]==']' and lastOpenParenthesis !='[') or (s[i]=='}' and lastOpenParenthesis !='{'):
                    return False
        return len(stack)==0

No comments :

Post a Comment