Level Up Your Coding Skills & Crack Interviews — Save up to 50% or more on Educative.io Today! Claim Discount

Arrow
Table of contents

20. Valid Parentheses

Problem Statement

You are given a string s that contains only the characters '(', ')', '{', '}', '[', and ']', Your task is to determine whether the string is valid.

A string is considered valid if all of the following hold:

  • Open brackets are closed by the same type of bracket.
  • They are closed in the correct order.
  • Every closing bracket has a corresponding opening bracket.

Return True if s is valid; otherwise, return False.

Constraints:

  • $1 \leq$ s.length $\leq 10^{4}$
  • s consists of parentheses only ‘()[]{}‘.

Examples

The examples below illustrate a few typical outcomes:

InputOutputExplanation
“{[]}”TrueEach opener is closed by the same type, and closures occur in the correct nesting order.
“([{})]”FalseA ) appears while ] is the next expected closer, so the nesting order breaks.
“(((())))[]{}”TrueAll groups are properly nested and fully closed, leaving no unmatched elements.

Optimal Solution: Stack-based Validation

The optimal approach is to maintain a stack of the closing brackets we expect to see next, which lets us verify correctness as we scan the string once. Whenever we encounter an opening bracket, we push its matching closing bracket onto the stack, recording what must appear later. When we encounter a closing bracket, it must match the stack’s top; otherwise, the order or type is incorrect, and the string is invalid. A closing bracket with an empty stack is also immediately invalid because there is nothing to match. After processing all characters, the stack must be empty; any remaining expected closers indicate that there are unmatched openings. This avoids any repeated scanning because every character is processed once.

This algorithm is implemented as follows:

  1. Initialize an empty stack expected.
  2. Scan characters of s from left to right.
  3. If the character is an opening bracket, push its matching closing bracket onto expected.
  4. Otherwise (it is a closing bracket):
    1. If the stack is empty, return False (nothing to match).
    2. Pop the top element and compare it to the current character; if they differ, return False.
  5. After the scan ends, return True only if the stack is empty (no unmatched openers remain).

Here is a visual representation of the algorithm discussed above:

1 / 6

Code Implementation

Time Complexity

The time complexity of this solution is $O(n)$ because each character is pushed to or popped from the stack at most once.

Space Complexity

The space complexity of this solution is $(n)$ because, in the worst case, when the string contains only opening brackets, the stack may hold up to $n$ expected closers.

Common Pitfalls

A few small habits prevent most mistakes on this problem:

  • Always guard against popping an empty stack: Any closing bracket that appears when the stack is empty must return False immediately.
  • Use a meaningful, consistent stack: Either store opening brackets (and check pairs on close) or store expected closing brackets. Mixing the two leads to subtle bugs.
  • Early exits are a feature, not an optimization trick: As soon as a mismatch occurs, the final answer cannot recover; returning immediately keeps the logic clean.
  • Quick edge checks help with debugging: Strings of odd length can never be valid, and a valid string must end with an empty stack.

Frequently Asked Questions

Why is a stack the natural choice here?

Because brackets must close in last-opened, first-closed order (LIFO), which is exactly what a stack enforces.

Can different bracket types be mixed within the same string?

Yes. Any mix is allowed; validity depends on correct matching and nesting.

Can valid strings contain multiple layers of nesting and multiple segments together?

Yes. A string like "({[]})(){}[()]" is valid if each nested and adjacent part follows the rules.

What are the most common bugs?

Forgetting the empty-stack check, mismatching bracket pairs, or returning True without verifying the stack is empty at the end.

Share with others:

Leave a Reply

Your email address will not be published. Required fields are marked *