본문 바로가기
Programming/LeetCode

[LeetCode] Valid Parentheses

by 데이터현 2022. 3. 25.

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

'Programming > LeetCode' 카테고리의 다른 글

[LeetCode] Number of Islands  (0) 2022.03.25
[LeetCode] Two City Scheduling  (0) 2022.03.25
[LeetCode] Swap Nodes in Pairs  (0) 2022.03.24
[LeetCode] Boats to Save People  (0) 2022.03.24
[LeetCode] Add Two Numbers  (0) 2022.03.23

댓글