문제

리트코드 20. Valid Parentheses
난이도: ⭐

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
(괄호로 된 입력값이 올바른지 판별하라)

예시 1

Input: s = "()"
Output: true

예시 2

Input: s = "([)]"
Output: false

나의 풀이: 스택을 이용한 풀이

class Solution:
    def isValid(self, s: str) -> bool:
        
        dict1 = {'(':')', '{':'}', '[': ']'}
        stack = []
        
        for i in s:
            if i in dict1.keys():
                stack.append(i)
            elif not stack or i != dict1[stack.pop()]:
                return False
        return not stack
------------------------------------------------
결과: 통과, 런타임 32ms  

Tags:

Categories:

Updated: