Skip to content

Valid Parentheses With a Stack
Step by Step

Valid parentheses pushes every open bracket. Every close bracket must match the top, and the stack must end empty.

Step through every bracket in both directions, then share a link to the step where the match failed.

What You Watch While Brackets Push and Pop

The stack, the top bracket and the match result change on every step.

Brackets close in the opposite order they open. Stack order remembers that.

CodeStory's visualiser shows the stack growing and popping with the item on top highlighted. Marked code lines follow every move, and you can step back, step forward or play.

Open Crossed pairs first. Then load Left open to see why the final check matters.

How Valid Parentheses Matches Every Bracket

Steps below follow the stack on your screen.

  1. 1Move through the string from left to right.
  2. 2Push every open bracket onto the stack.
  3. 3Pop the top when a close bracket arrives, and stop if the two do not match.
  4. 4End with an empty stack. Anything left over means an open bracket never closed.

Valid Parentheses Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Bug: no final checkReturns true at the end without looking at the stack, so brackets that were opened and never closed slip through. Load Left open to see it.

Valid Parentheses Inputs Worth Trying

Three presets cover crossed pairs, open brackets and a single bracket.

  • Crossed pairsEvery bracket has a partner of its kind, but the pairs cross.
  • Left openEvery close matches, but two opens never close. Switch on "Bug: no final check" here.
  • One bracketSwitch on the no-final-check bug here too.

Valid Parentheses Cost in Time and Space

Every bracket is pushed once and popped once, so the work stays linear.

Time
O(n)counted from the pushes and pops you step through
Space
O(n)the stack that holds open brackets
Pattern
StackThe last open bracket is the first one to close.

Practise Stacks Beyond Parentheses

Watching shows the stack and solving trains you to match pairs in order.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Valid Parentheses Visualiser FAQs

Open the visualiser and follow the stack bracket by bracket.

Open the visualiser
What does the valid parentheses visualiser show?

CodeStory's visualiser shows the stack growing and popping with the item on top highlighted, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of valid parentheses?

O(n) time and O(n) space. Live counts show one push and one pop per bracket.

Why does my valid parentheses code fail on some inputs?

Skipping the final stack check is a common cause. Switch the bug on in the visualiser and watch open brackets slip through.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Stacks? Get a Free Callback

Tell CodeStory which stack step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from stack basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.