Array Methods

These are the few array methods that I had issues working with while starting out my javascript journey. Let’s quickly look at them and see how to effectively use them in development. As the name…


独家优惠奖金 100% 高达 1 BTC + 180 免费旋转

Stacks and Parentheses

Today’s problem is the ever-popular parentheses checker.

Given a string of parentheses, validate that the string has simple, balanced parentheses. Or in other words, validate that the parentheses are in order, and are properly nested.

My initial thought was to compare the current element to the one previous, but that wouldn’t work. Why not? Well, just because you have open parentheses, “(”, doesn’t mean that a closed one, “)”, needs to immediately follow. Think of “((()))”. Remember those insane algebra problems that tested the order of operations in elementary school? No? Just me?

an image of Buzz Lightyear and Woody with the line “Parentheses, parentheses everywhere” on it

There was one element of truth in my initial thought — which was to process the symbols from the left to the right.

So what makes a string of parentheses valid? The most recent opening parenthesis must match the next closing symbol, and the first opening parenthesis may have to wait until the very last symbol for its match → “(()())”. Speaking of the most recent opening parenthesis matching the next closing symbol.. you could also think of it as the closing symbols match the opening symbols in reverse order of their appearance. Taking these points into account, we can use a stack.

A quick review of a stack — a stack is a last-in, first-out data structure. Meaning the last item added to the stack is the only one that can be removed.

As we check the string from left to right, if we come across an opened parenthesis, we’ll go ahead and add it to the stack. If we hit a closed parenthesis, then we’ll pop off the last item from the stack, because we have a match! Our stack will only hold the opening parenthesis.

Let’s start setting it up:

We’ve set up our for loop for processing elements from left to right, and we’ve set up our stack as an empty array. We’ve also set up an if-else statement to add an open parenthesis to the parens stack if the element is a “(“. Now we need to handle the closing parenthesis. If the element is a “)”, then we can pop off the last item from the parens stack. To pop an element off the stack, we first want to be sure there is something in the stack. So we added another if-else statement as a check.

One last thing before we’re done! If we come out of this for loop and there are still open parentheses in the parens stack, then we know that it’s not simply balanced (there weren’t enough closed parentheses to finish it out). An example is “(()”.So we’ll add in another line for that check.

There you have it, folks! I’ve found stacks to be helpful for when you need a data structure to hold elements so that you can run a quick validation check on the previous element. Can you think of how to expand this to allow for other types of parentheses, like “[“ or “{“?

Add a comment

Related posts:

Four YouTube Channels To Subscribe To For Learning Entertainment

Out of all the streaming and entertainment packages I subscribe to, YouTube is my main source of video entertainment. Let me tell you why. If you want to keep up to date and informed fast, you watch…

WooCommerce Shortcodes Usage Guide

WooCommerce WordPress plugin comes with several shortcodes that can be used to insert content inside posts and pages. See here a list of the WooCommerce shortcodes. WooCommerce is a widely used free…

An Uber Problem

Austin has always been a city with a mind of its own. Described by many as “Portland’s sister city in weirdness,” it is a liberal haven in a sea of deep-red Texas. From disagreements on the state of…