๐Ÿ“ฆ Adib234 / Leetcode

๐Ÿ“„ valid_parentheses.py ยท 22 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []

        dict_check = {"}": "{", ")": "(", "]": "["}
        for char in s:

            if char in dict_check:
                top = stack.pop() if stack else '#'

                if top != dict_check[char]:
                    return False

            else:
                stack.append(char)

        return not stack