Showing posts with label Algorithm problems. Show all posts
Showing posts with label Algorithm problems. Show all posts

Sunday, April 9, 2017

Find number of islands from a give 2D matrix of 0s and 1s

For a given 2D matrix of 0s and 1s, find number of islands where an island is defined as number of 1s together. For example for a 2D matrix below:
0,1,1,0
0,1,1,0
0,0,0,0
1,1,0,0

Number of islands is 2.

Solution is going to O(m*n) where m and n are number or rows and columns of the matrix respectively.

    int numIslands(int [][]a) {
        if (a == null || a.length == 0 || a[0].length == 0) {
            return 0;
        }

        int rows = a.length;
        int cols = a[0].length;
        boolean[][]b = new boolean[rows][cols];
        int counter = 0;

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                if (a[i][j] == 1 && !b[i][j]) {
                    counter ++;
                    visitIsland(a, b, i, j);
                }

        return counter;
    }

    void visitIsland(int[][]a, boolean[][]b, int i, int j) {
        if (a == null || b == null
                || a.length == 0 || b.length == 0
                || a[0].length == 0 || b[0].length == 0
                || i < 0 || j < 0 || i == a.length || j == a[0].length)
            return;

        b[i][j] = true;

        if (i + 1 < a.length && a[i+1][j] == 1 && !b[i + 1][j])
            visitIsland(a, b, i + 1, j);

        if (j + 1 < a[0].length && a[i][j+1] == 1 && !b[i][j+1])
            visitIsland(a, b, i, j + 1);

        if (i - 1 >= 0 && a[i-1][j] == 1 && !b[i - 1][j])
            visitIsland(a, b, i - 1, j);

        if (j - 1 >= 0 && a[i][j-1] == 1 && !b[i][j-1])
            visitIsland(a, b, i, j-1);
    }

Sunday, February 1, 2015

Regular expression implementation for '*' and '?' wildcard characters

Assuming we have two string - a normal string which will be matched against a regular expression string. The idea is simple: iterate through both string simultaneously. If:

  • current char in regex is '?', skip current characters from both strings
  • current char in regex is '*', find the next matching character in original string
  • else match the current character.

The implementation of the above idea is captured in code at the link

Saturday, September 10, 2011

Startup comment

I will post a new algorithm problem regularly. I don't guarantee daily, but at least once a week. I will also try to post a possible hint, but that does not mean it will lead to optimal solution. As I mentioned, it will be just a hint. I welcome comments (as possible solutions) to the question/problem.
I understand that there are other sites that might be doing the same. But my intention of this blog post is to keep the people around the world provide valuable insights to others and share knowledge...in benefit to others. Who knows, together, we can take this post to new heights where it becomes really valuable and unique.
All ideas for improvements are welcome.
Let's help each other! :)