Applied AI Engineer (LLM) coding learning path Level 1 This level · 4 of 9

Valid Parentheses

Easy Stack String

Problem

Given a string of the characters '(', ')', '{', '}', '[' and ']', return true if the brackets are closed in the correct order and every opener has a matching closer.

Original: LeetCode 20: Valid Parentheses.

Examples

Example 1

Input`s = "()"`
Output`true`

Example 2

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

Example 3

Input`s = "(]"`
Output`false`

Example 4

Input`s = "["`
Output`false`

Example 5

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

Constraints

  • 1 <= s.length <= 10^4
  • s consists of parentheses only '()[]{}'.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (3)
  1. LeetCode 20: Valid Parenthesesleetcode.com
  2. Java SE 17 API: ArrayDequedocs.oracle.com
  3. HackerRank: Balanced Bracketshackerrank.com
Previous
Next