Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 519 Bytes

020.md

File metadata and controls

24 lines (22 loc) · 519 Bytes

20. Valid Parentheses

Solution 1 (O(n))

# Stack
class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        d={')':'(', '}':'{', ']':'['}
        for c1 in s:
            if c1 in d.keys():
                if not stack or stack.pop() != d[c1]:
                    return False
            else:
                stack.append(c1)
        if stack:
            return False
        return True