Skip to content
EasyStacks QueuesFree practice

Valid Parentheses

Asked atgoogleamazonmetamicrosoftbloomberg

01 · Problem

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.

02 · Examples

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

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

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

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

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

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

03 · Constraints

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

04 · Optimal complexity

Time
O(n)
Space
O(n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.