Hey folks, Leo here.

This is a classic algorithm problem and the solution is very straightforward. The problem:

Given a string of parenthesis ‘(‘ and ‘)’, write a function that returns true if there are matching pairs and false if there are not. A matching pair means, there should be a closing parenthesis for an opening one, incorrect order.

The easiest solution is using a stack data structure. Check the solution below:  

func isMatchinParenthesis(phrase: String) -> Bool {
    var resultStack : [String.Element] = []
    if phrase.isEmpty || phrase.count < 2 {
        return false
    for letter in phrase {
        if letter == "(" {
        } else if letter == ")" {
            if resultStack.last == "(" {
            } else {
                return false
    return resultStack.isEmpty

You can use the switch statement rather than the ifs. There are others ways to accomplish this, can you find another one?


Continue Algorithm Studying 

The Trie is a data structure that can be used to automatically make the auto-correction feature of the digital keyboards. In this article, you will learn how to make that.

If you are into graph algorithms don’t miss this article where we solve the “Number of Islands Problem”. There we use BFS to solve a hard Leetcode question.



That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for the reading and… That’s all folks.

Image Credit: Wikiart