Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

0020 - Valid Parentheses #277

@jmdana

Description

@jmdana

Description of the Problem

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Code

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        openbrackets = ('(', '[', '{')
        matches = ('()', '[]', '{}')

        for c in s:
            if c in openbrackets:
                stack.append(c)
            else:
                if len(stack) < 1 or stack[-1] + c not in matches:
                    return False

                stack.pop()

        return len(stack) == 0

Link To The LeetCode Problem

LeetCode

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions