<aside> 💡 Dumb and possibly pretty stressful queue for the real world but really important when it comes to programming

</aside>


How does it work?

Here, the last element always comes out first, so to speak, the ones on top.

<aside> 💡 Stack

</aside>


Real life

Imagine you have a stack of plates. Which one would you grab first? If you don’t feel like breaking some of them, you’d probably reach for the one on top.


The Parenthesis problem

The "parenthesis problem" typically refers to the task of determining whether a given expression has balanced parentheses. In other words, you need to check if every opening parenthesis has a corresponding closing parenthesis, and they are properly nested. For example, the expressions "(())" and "(()())" are balanced, while "())(" is not.

One way to solve this problem is by using a data structure called a stack. A stack is a Last In, First Out (LIFO) data structure, where elements are added and removed from the same end, usually called the "top" of the stack.

Here's a general approach to solving the balanced parentheses problem using a stack:

  1. Initialize an empty stack.
  2. Traverse the given expression from left to right.
  3. For each opening parenthesis ('('), push it onto the stack.
  4. For each closing parenthesis (')'), check if the stack is empty. If it is, the expression is unbalanced. Otherwise, pop the top element from the stack.
  5. After traversing the entire expression, check if the stack is empty. If it is, the parentheses are balanced; otherwise, the expression is unbalanced.