/20. Valid Parentheses

20. Valid Parentheses

Easy
Strings43.8% acceptance

Given a string input_str consisting only of the characters (, ), {, }, [ and ], determine if input_str is a valid sequence of brackets. A valid sequence is defined as one where every opening bracket is closed by the same type of bracket and in the correct order, and every closing bracket has a corresponding opening bracket of the same type.

Example 1

Input: {{[()]}}

Output: True

Explanation: All brackets are properly nested and closed.

Example 2

Input: ([{}])

Output: True

Explanation: Brackets are correctly nested and closed.

Example 3

Input: ([{})]

Output: False

Explanation: Brackets are not closed in the correct order.

Example 4

Input: (((

Output: False

Explanation: Not all opening brackets are closed.

Example 5

Input: [({})]{}

Output: True

Explanation: All brackets are properly nested and closed.

Constraints

  • 1 <= len(input_str) <= 10_000
  • input_str contains only characters (, ), {, }, [, ]
Python (current runtime)

Case 1

Input: '{}[]()'

Expected: True

Case 2

Input: '([)]'

Expected: False

Case 3

Input: '(([]){})'

Expected: True

Case 4

Input: '([)'

Expected: False

Case 5

Input: '[({})]({[]})'

Expected: True