<p>Given a 2D array of characters <code>grid</code> of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong> in <code>grid</code>.</p>

<p>A cycle is a path of <strong>length 4 or more</strong> in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p>

<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle <code>(1, 1) -&gt; (1, 2) -&gt; (1, 1)</code> is invalid because from <code>(1, 2)</code> we visited <code>(1, 1)</code> which was the last visited cell.</p>

<p>Return <code>true</code> if any cycle of the same value exists in <code>grid</code>, otherwise, return <code>false</code>.</p>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" />
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;c&quot;,&quot;c&quot;,&quot;c&quot;,&quot;a&quot;],[&quot;c&quot;,&quot;d&quot;,&quot;c&quot;,&quot;c&quot;],[&quot;c&quot;,&quot;c&quot;,&quot;e&quot;,&quot;c&quot;],[&quot;f&quot;,&quot;c&quot;,&quot;c&quot;,&quot;c&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" />
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;z&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;a&quot;]]
<strong>Output:</strong> false
</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;= 500</code></li>
	<li><code>grid</code> consists only of lowercase English letters.</li>
</ul>

class DisjointSet
{
    vector<int>rank,parent;
public:
    DisjointSet(int N)
    {
        rank.resize(N+1,0);
        parent.resize(N+1);
        for(int i=0;i<=N;i++)
        {
            parent[i]=i;
        }
    }

    int findUPar(int Node)
    {
        if(Node==parent[Node])
        {
            return Node;
        }
        return parent[Node]=findUPar(parent[Node]);
    }

    void unionByRank(int u, int v)
    {
        int ulp_u=findUPar(u);
        int ulp_v=findUPar(v);
        if(ulp_u==ulp_v)
        {
            return;
        }
        if(rank[ulp_u]<rank[ulp_v])
        {
            parent[ulp_u]=ulp_v;
        }
        else if(rank[ulp_v]<rank[ulp_u])
        {
            parent[ulp_v]=ulp_u;
        }
        else
        {
            parent[ulp_u]=ulp_v;
            rank[ulp_v]++;
        }
    }
};
class Solution {
public:
    bool containsCycle(vector<vector<char>>& grid) {
        int N=grid.size();
        int M=grid[0].size();
        vector<pair<char,pair<int,int>>>V;
        vector<vector<int>>W(N,vector<int>(M));
        int X=0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++){
                X++;
                W[i][j]=X;
            }
        }
        set<pair<char,pair<int,int>>>Z;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[i].size();j++){
                char col=grid[i][j];
                if(j+1<M && grid[i][j+1]==col)
                {
                    Z.insert({col,{W[i][j],W[i][j+1]}});
                }
                if(j-1>=0 && grid[i][j-1]==col)
                {
                    Z.insert({col,{W[i][j-1],W[i][j]}});
                }
                if(i+1<N && grid[i+1][j]==col)
                {
                    Z.insert({col,{W[i][j],W[i+1][j]}});
                }
                if(i-1>=0 && grid[i-1][j]==col)
                {
                    Z.insert({col,{W[i-1][j],W[i][j]}});
                }
            }
        }
        map<char,vector<pair<int,int>>>M1;
        for(auto it : Z)
        {
            M1[it.first].push_back({it.second.first,it.second.second});
        }
        bool flag=0;
        for(auto it : M1)
        {
            DisjointSet ds(N*M);
            bool flag1=0;
            for(auto it1 : M1[it.first])
            {
                int ulp_u=ds.findUPar(it1.first);
                int ulp_v=ds.findUPar(it1.second);
                if(ulp_u==ulp_v)
                {
                    flag1=1;
                    break;
                }
                else
                {
                    ds.unionByRank(it1.first,it1.second);
                }
            }
            if(flag1==1)
            {
                flag=1;
                break;
            }
        }
        if(flag==1){
            return true;
        }
        return false;
    }
};
144883