ntree.ai
ntree.aiValid Brackets

Valid Brackets

easysolvedattempted

You are given a string s of brackets (, ), [, ], { and }.

Return true if the string is valid.

A string of brackets is valid when:

  • every opening bracket is closed by a bracket of the same type
  • open brackets are closed in the correct order.
  • every closing bracket closes a bracket that was opened before it

Example 1:

Input:  s = "{[()]}"
Output: true

Example 2:

Input:  s = "([)]"
Output: false

Example 3:

Input:  s = "())"
Output: false

Constraints:

  • 1 <= length of s <= 10^4
  • s consists of the characters (, ), [, ], { and }
def isValid(s: str) -> bool:
pass
 

Press Visualise to step through your code.