파이썬 알고리즘 인터뷰 문제 20번(리트코드 20번) Valid Parentheses
https://leetcode.com/problems/valid-parentheses/
class Solution:
def isValid(self, s: str) -> bool:
stack = []
close_brackets = {')':'(', '}':'{', ']':'['}
for char in s:
if char in close_brackets:
if not stack or stack.pop() != close_brackets[char]: # empty stack case
return False
else:
stack.append(char)
return not stack
stack 비어 있는 경우를 생각하지 못하여 not stack 조건을 누락했었다. Stack을 이용할 때는 비어있는 경우를 빠트리지 말자.