leetcode 020

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.

Method

Use stack.

Solution

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        res = []
        for i in s:
            if i == "[" or i == "{" or i == "(":
                res.append(i)
            elif i == "]" :
                if res == [] or res.pop() != "[": return False
            elif i =="}":
                if res == [] or res.pop() != "{": return False
            elif i ==")":
                if res == [] or res.pop() != "(": return False
        if  res == []: return True
        else: return False