20. 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.

    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        hm = {'{':'}','(':')','[':']'}
           keys = hm.keys()
        stack = []
        for i in s:
            if i in keys:
                stack.append(i)
            else:
                if not stack or hm[stack[-1]] != i:
                    return False
                stack.pop()

        return len(stack) == 0

results matching ""

    No results matching ""