Mark As Completed Discussion

Stack to the Rescue

The easiest way to accomplish what we want is with a stack data structure, as its push() and pop() methods model the pair-wise opposing symbols in an efficient manner (the push/pop operations for most stack implementations is O(1)).

With a simple stack in place, we can start by iterating through the given string.

At each step, look for any opening/left-side symbol and push it onto the stack. Here's a sample snippet of what that looks like.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment