Menu

I would like to share with you a very simple but interesting algorithm that you could one day use or could be just as interesting to you as it was to me at that time.

We were discussing about lists one day at my first boot camp and then we were given a coding exercise on how we would validate a string if it was properly closed.

Properly Closed String

valid = "(I (a(m)) a String)"

Improperly Closed String

invalid = "(I (a(m) a String)"

At that time we were just talking about parenthesis and brackets. We could still apply this to HTML tags (we’ll see that later).

When our instructor showed us one way of doing this. He told us

“You can use the idea of STACKS to check if the string is properly closed or not”

So here we go!

The Algorithm

Create a list the will act as our STACK

I think this is pretty straightforward. Let’s create a list called.. stack. Let’s put it inside a checkTag function that receives a string called myString and also create a string to pass for this function