본문 바로가기
코드

Valid Parentheses

by ehei 2020. 2. 12.

https://leetcode.com/problems/valid-parentheses/

괄호가 잘 닫혔는지 검사하는 문제

 

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        pairs = {
            ')' : '(',
            ']' : '[',
            '}' : '{',
        }
        
        for ch in s:
            if ch in ( '(', '[', '{' ):
                stack.insert( 0, ch )
            else:
                if not stack:
                    return False
                else:
                    openBracket = stack.pop( 0 )
                    
                    if pairs[ ch ] != openBracket:
                        return False
                    
        return not stack

'코드' 카테고리의 다른 글

테트리스  (0) 2020.04.13
django 과제  (0) 2020.02.22
Min Stack  (0) 2020.02.12
Target Sum  (0) 2019.12.04
Clone Graph  (0) 2019.11.26