|Valid Parentheses
All Challenges

Valid Parentheses

MediumStacks

Given a string s containing only the characters (, ), {, }, [, ], return True if the string is valid.


A string is valid if:

1. Every opening bracket has a matching closing bracket of the same type

2. Brackets are closed in the correct order


This is exactly what your code editor uses to highlight mismatched brackets.

Examples

Input: valid_parentheses("()")
Output: True
Simple match
Input: valid_parentheses("()[]{}")
Output: True
Multiple types, all matched
Input: valid_parentheses("(]")
Output: False
Wrong closing bracket
Input: valid_parentheses("([)]")
Output: False
Out of order
Input: valid_parentheses("{[]}")
Output: True
Nested correctly

Constraints

  • 1 <= len(s) <= 10,000
  • s contains only ()[]{}
pythoneditor
Loading Python runtime…