Soohan-Kim / Leetcode

0 stars 0 forks source link

[HW 4] Valid Parentheses #20. #47

Open Soohan-Kim opened 3 weeks ago

Soohan-Kim commented 3 weeks ago

https://leetcode.com/problems/valid-parentheses/description/

Soohan-Kim commented 3 weeks ago
class Solution:
    def isValid(self, s: str) -> bool:

        if len(s) % 2:
            return 0

        l = deque()

        for c in s:
            if c in ['(', '[', '{']:
                l.append(c)
            else:
                if len(l) == 0:
                    return 0
                if c == ')' and l[-1] == '(':
                    l.pop()
                elif c == ']' and l[-1] == '[':
                    l.pop()
                elif c == '}' and l[-1] == '{':
                    l.pop()
                else:
                    return 0
        if len(l):
            return 0
        return 1