Lintcode 423. Valid Parentheses (Easy)(Python)

423. Valid Parentheses

Description

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

Example
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

Code:

class Solution:
    """
    @param s: A string
    @return: whether the string is a valid parentheses
    """
    def isValidParentheses(self, s):
        # write your code here
        dict = ['a']
        if len(s)%2 != 0:
            return False
        var = {')':'(', ']':'[', '}':'{'}
        for i in s:
            if i in var :
                if var[i]!=dict.pop():
                    return False
            else:
                dict.append(i)
        if len(dict) != 1:
            return False
        else:
            return True

猜你喜欢

转载自blog.csdn.net/weixin_41677877/article/details/80948254