<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p>

<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]
]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]
]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution {
public:
    class DisjointSet
    {
        public:
        vector<int> parent, rank, size;
        int count;
        DisjointSet(int n)
        {
            rank.resize(n+1, 0);
            size.resize(n+1, 1);
            parent.resize(n+1);
            count = 0;
            for(int i = 0;i<=n;i++)
                parent[i] = i;
        }
        int find(int x)
        {
            if(x==parent[x])
                return x;
            return parent[x] = find(parent[x]);
        }
        void union_by_rank(int x, int y)
        {
            int par_x = find(x);
            int par_y = find(y);
            if(par_x==par_y)
                return;
            else
            {
                if(rank[par_x]>rank[par_y])
                    parent[par_y] = par_x;
                else if(rank[par_x]<rank[par_y])
                    parent[par_x] = par_y;
                else
                {
                    parent[par_y] = par_x;
                    rank[par_x]++;
                }
            }
        }
        void union_by_size(int x, int y)
        {
            int par_x = find(x);
            int par_y = find(y);
            if(par_x==par_y)
                return;
            else
            {
                if(size[par_x]>size[par_y])
                {
                    parent[par_y] = par_x;
                    size[par_x] += size[par_y];
                }
                else
                {
                    parent[par_y] = par_x;
                    size[par_x] += size[par_y];
                }
                count--;
            }
        }
        void increment_count()
        {
            count++;
        }
    };
    int numIslands(vector<vector<char>>& grid) {
        int r = grid.size();
        int c = grid[0].size();
        DisjointSet ds(r*c);
        vector<int> directions = {0, 1, 0};
        
        for(int i = 0;i<grid.size();i++)
        {
            for(int j = 0;j<grid[0].size();j++)
            {
                char curr = grid[i][j];
                if(curr=='1')
                {
                    ds.increment_count();
                    for(int k = 0;k<directions.size()-1;k++)
                    {
                        int new_r = i + directions[k];
                        int new_c = j + directions[k+1];
                        if(new_r>=0 && new_r<r && new_c>=0 && new_c<c && grid[new_r][new_c]=='1')
                        {
                            int hash_for_curr = i*c+j;
                            int hash_for_new = new_r*c + new_c;
                            ds.union_by_size(hash_for_curr, hash_for_new);
                        }
                    }
                }
            }
        }
        return ds.count;
    }
};
17244