<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 Solution {
public:
    bool contains(stack<pair<int, int>> s, pair<int, int> value) {
        stack<pair<int, int>> temp;
        bool found = false;

        while (!s.empty()) {
            pair<int, int> top = s.top();
            s.pop();
            temp.push(top);
            if (top == value) {
                found = true;
                break;
            }
        }

        // Restore the original stack
        while (!temp.empty()) {
            s.push(temp.top());
            temp.pop();
        }

        return found;
    }

    bool lookForCycle(stack<pair<int, int>>& toVisit, vector<vector<bool>>& visited, int i, int j, char toFind, vector<vector<char>>& grid) {
        toVisit.push({i, j});
        visited[i][j] = true;

        // Set to keep track of visited cells in the current path
        set<pair<int, int>> path;

        while (!toVisit.empty()) {
            pair<int, int> dot = toVisit.top();
            toVisit.pop();
            i = dot.first;
            j = dot.second;

            if (path.count(dot)) {
                // Found a cycle
                return true;
            }
            path.insert(dot);

            // Check adjacent cells
            if (i - 1 >= 0 && grid[i - 1][j] == toFind) {
                if (visited[i - 1][j] && !path.count({i - 1, j})) return true;
                if (!visited[i - 1][j]) {
                    toVisit.push({i - 1, j});
                    visited[i - 1][j] = true;
                }
            }
            if (i + 1 < grid.size() && grid[i + 1][j] == toFind) {
                if (visited[i + 1][j] && !path.count({i + 1, j})) return true;
                if (!visited[i + 1][j]) {
                    toVisit.push({i + 1, j});
                    visited[i + 1][j] = true;
                }
            }
            if (j - 1 >= 0 && grid[i][j - 1] == toFind) {
                if (visited[i][j - 1] && !path.count({i, j - 1})) return true;
                if (!visited[i][j - 1]) {
                    toVisit.push({i, j - 1});
                    visited[i][j - 1] = true;
                }
            }
            if (j + 1 < grid[0].size() && grid[i][j + 1] == toFind) {
                if (visited[i][j + 1] && !path.count({i, j + 1})) return true;
                if (!visited[i][j + 1]) {
                    toVisit.push({i, j + 1});
                    visited[i][j + 1] = true;
                }
            }
        }

        return false;
    }

    bool containsCycle(vector<vector<char>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        vector<vector<bool>> visited(row, vector<bool>(col, false));
        stack<pair<int, int>> toVisit;

        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (!visited[i][j]) {
                    char toFind = grid[i][j];
                    if (lookForCycle(toVisit, visited, i, j, toFind, grid)) return true;
                }
            }
        }

        return false;
    }
};

83226