SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Valid Parentheses

Use a stack to validate nested brackets and parentheses.

Try It Yourself

Use the editor below to validate the order and type of every closing symbol.

Track unmatched opening symbols

Store opening symbols until their matching closing symbols arrive. Check incorrect nesting, extra opening symbols, and a closing symbol with no opener.

Submit your implementation when it passes the examples.

Valid Parentheses

easyStack · Strings20 minLC 20
Problem

Given a string containing only parentheses, brackets, and braces, return true when every opening symbol is closed by the same type in the correct order.

An empty stack at the end means every opening symbol was matched.

Example 1:

Input: s = "()[]{}"
Output: true
Explanation: Each opening symbol is immediately followed by its matching closing symbol, so all three pairs are valid.

Example 2:

Input: s = "([)]"
Output: false
Explanation: The first closing parenthesis appears while the square bracket is still open. The pairs cross, so the string is invalid.

Constraints

  • 1 ≤ s.length ≤ 10,000
  • s contains only ()[]{}.
0 attempts

Test results

Results appear here after a run.

Solution

Three nested opening symbols are pushed in order and matched by popping them in reverse order.

Push every opening symbol onto a stack. For a closing symbol, pop the most recent opener and verify that it is the required matching type.

In ([)], the first closing ) meets [ at the top. Return false immediately even though the numbers of opening and closing symbols agree. A closer with an empty stack also fails. After the last symbol, the stack must be empty for the string to be valid.

function isValidParentheses(s) {
  const expectedOpening = new Map([
    [")", "("],
    ["]", "["],
    ["}", "{"],
  ]);
  const stack = [];

  for (const symbol of s) {
    if (!expectedOpening.has(symbol)) {
      stack.push(symbol);
      continue;
    }

    if (stack.pop() !== expectedOpening.get(symbol)) {
      return false;
    }
  }

  return stack.length === 0;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Every symbol is inspected and pushed or popped at most once.
Auxiliary spaceO(n)An input containing only opening symbols fills the stack.

How is this practice?

Previous lessonStackNext lessonMin Stack
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation