LeetCode/Grind169

20. Valid Parentheses

hyunkookim 2025. 4. 22. 01:45

20. Valid Parentheses

class Solution:
    def isValid(self, s: str) -> bool:
        charDict = {')':'(', '}':'{', ']':'['}

        stack = []

        for w in s:
            if w in charDict and stack:                
                if stack[-1] != charDict[w]:
                    return False
                else:
                    stack.pop()
            else:
                stack.append(w)

        # 여기까지 오고, stack 이 비어있지 않으면 False
        return True if not stack else False