20. Valid Paretheses

김민관·2022년 9월 14일

백준_Silver

목록 보기
40/57

파이썬

class Solution(object):
    def isValid(self, s):
        stack = []

        for i in s:
            if i == '(':
                stack.append(i)
            elif i == ')':
                if stack and stack[-1] == '(':
                    stack.pop()
                else:
                    stack.append(i)

            if i == '[':
                stack.append(i)
            elif i == ']':
                if stack and stack[-1] == '[':
                    stack.pop()
                else:
                    stack.append(i)

            if i == '{':
                stack.append(i)
            elif i == '}':
                if stack and stack[-1] == '{':
                    stack.pop()
                else:
                    stack.append(i)

        if stack:
            return False
        else:
            return True

풀이

  • 백준에 있는 stack "괄호" 문제와 같은 유형
profile
게임 개발일지 & IT 소식들 공유

0개의 댓글