All problems
EasyStacks Queues

Valid Parentheses

googleamazonmetamicrosoftbloomberg

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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
  3. Every close bracket has a corresponding open bracket of the same type.

Examples

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

Each opening bracket is matched by the same type of closing bracket in the correct order.

Example 2
Input
s = "([)]"
Output
false

The brackets are not closed in the correct order — the '[' is closed by ')' before the '(' is closed.

Example 3
Input
s = "{[]}"
Output
true

The '[' inside '{}' is properly closed before the outer '{' is closed.

Constraints

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

Optimal complexity

Time

O(n)

Space

O(n)

One problem, two ways to prep

Choose between solo practice and interview simulation

Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.