LC 0020 [E] Valid Parentheses - ALawliet/algorithms GitHub Wiki

class Solution:
    def isValid(self, s: str) -> bool:
        l = []
        r = []
        
        for x in s:
            if x in '({[[':
                l.append(x)
            else:
                if x == ')':
                    if l and l[-1] == '(':
                        l.pop()
                    else:
                        return False
                elif x == '}':
                    if l and l[-1] == '{':
                        l.pop()
                    else:
                        return False
                elif x == ']':
                    if l and l[-1] == '[':
                        l.pop()
                    else:
                        return False
                    
        return not l
class Solution:
    def isValid(self, s: str) -> bool:
        opened = ['[', '(', '{']
        closed = [']', ')', '}']
        stack = []
        for c in s:
            if c in opened:
                stack.append(c)
            else:
                if stack and stack[-1] == opened[closed.index(c)]:
                    stack.pop()
                else:
                    return False
        return not stack