Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 804 Bytes

README.md

File metadata and controls

25 lines (20 loc) · 804 Bytes

Valid Parentheses

Problem can be found in here!

def isValid(s: str) -> bool:
    stack = []
    bracket = {"(": ")", "[": "]", "{": "}"}
    for char in s:
        # Case 1: Left Bracket, just simply push into stack
        if char in set("([{"):
            stack.append(char)
        # Case 2: Right Bracket, may cause invalid parentheses
        else:
            if not stack or bracket[stack[-1]] != char:
                return False
            else:
                stack.pop()

    return not stack

Time Complexity: O(n), Space Complexity: O(n)