Programming/LeetCode
[LeetCode] Valid Parentheses
데이터현
2022. 3. 25. 16:04
https://leetcode.com/problems/valid-parentheses/
Valid Parentheses - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
괄호가 정상적인지 찾는 스택의 대표 문제
class Solution:
def isValid(self, s: str) -> bool:
types = { ')' : '(', ']' : '[', '}': '{' }
stack = []
for par in s:
if par in types:
if not stack or types[par] != stack.pop():
return False
else:
stack.append(par)
return len(stack) == 0