Skip to Main Content

Valid Parentheses

Problem URL:Valid Parentheses

My Solution:

def isValid(s: str) -> bool:
    mappings = {
        "(": ")",
        "{": "}",
        "[": "]"
    }
    stack = []

    for parenthesis in list(s):
        if parenthesis in mappings:
            stack.append(parenthesis)
            continue
        
        if len(stack) > 0 and mappings[stack[-1]] == parenthesis:
            stack.pop()
        else:
            return False
            

    return False if (len(stack) > 0 or len(s) <= 1) else True

Let's Connect

Twitter GitHub LinkedIn