본문 바로가기

IT/코딩테스트

[Leetcode] 678. Valid Parenthesis String

728x90

https://leetcode.com/problems/valid-parenthesis-string

class Solution:
    def checkValidString(self, s: str) -> bool:

        stack = []
        star = []

        for i, c in enumerate(s):
            if c == '(':
                stack.append(i)
            elif c == '*':
                star.append(i)
            elif c == ')':
                if stack:
                    stack.pop()
                elif star:
                    star.pop()
                else:
                    return False

        while stack and star:
            if stack[-1] > star[-1]:
                return False
            stack.pop()
            star.pop()

        if stack:
            return False
        
        return True
728x90