[LeetCode] 20. Valid Parentheses

김민우·2023년 4월 10일
0

- Problem

20. Valid Parentheses

- 내 풀이 (stack)

class Solution:
    def isValid(self, s: str) -> bool:
        brackets = []
        mapping = {")": "(", "}": "{", "]": "["}
        
        for c in s:
            if c in mapping:
                last_bracket = brackets.pop() if brackets else 'EMPTY'
                if mapping[c] != last_bracket:
                    return False
            else:
                brackets.append(c)
        
        return not brackets

- 결과

  • 시간 복잡도: O(N)
  • 공간 복잡도: O(N)
profile
Pay it forward.

0개의 댓글