Problem link
https://leetcode.com/problems/valid-parentheses/
Problem Summary
괄호들이 올바른지 판단하는 문제
Solution
매우 간단한 스택 문제이다.
스택에 여는 괄호를 넣고 닫힌 괄호가 나오면 스택에서 빼면서 체크하면 된다. 마지막에 스택이 비어있는지도 체크한다.
Source Code
class Solution:
def isValid(self, s: str) -> bool:
close = {')': '(', '}': '{', ']': '['}
stack = []
for char in s:
if char in close:
if not stack or stack.pop() != close[char]:
return False
else:
stack.append(char)
return not stack