java.lang.Object
g1501_1600.s1559_detect_cycles_in_2d_grid.Solution

public class Solution extends Object
1559 - Detect Cycles in 2D Grid.<p>Medium</p> <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) -> (1, 2) -> (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><strong>Example 1:</strong></p> <p><strong><img src="https://assets.leetcode.com/uploads/2020/07/15/1.png" alt="" /></strong></p> <p><strong>Input:</strong> grid = [[&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;],[&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;a&rdquo;],[&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;a&rdquo;],[&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> There are two valid cycles shown in different colors in the image below: <img src="https://assets.leetcode.com/uploads/2020/07/15/11.png" alt="" /></p> <p><strong>Example 2:</strong></p> <p><strong><img src="https://assets.leetcode.com/uploads/2020/07/15/22.png" alt="" /></strong></p> <p><strong>Input:</strong> grid = [[&ldquo;c&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;,&ldquo;a&rdquo;],[&ldquo;c&rdquo;,&ldquo;d&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;],[&ldquo;c&rdquo;,&ldquo;c&rdquo;,&ldquo;e&rdquo;,&ldquo;c&rdquo;],[&ldquo;f&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> There is only one valid cycle highlighted in the image below: <img src="https://assets.leetcode.com/uploads/2020/07/15/2.png" alt="" /></p> <p><strong>Example 3:</strong></p> <p><strong><img src="https://assets.leetcode.com/uploads/2020/07/15/3.png" alt="" /></strong></p> <p><strong>Input:</strong> grid = [[&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;],[&ldquo;b&rdquo;,&ldquo;z&rdquo;,&ldquo;b&rdquo;],[&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;a&rdquo;]]</p> <p><strong>Output:</strong> false</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 <= m, n <= 500</code></li> <li><code>grid</code> consists only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • containsCycle

      public boolean containsCycle(char[][] grid)