Detect Cycles in 2D Grid

Medium · Graphs · about 20 minutes

Asked at Amazon, Google, Microsoft.

Given a grid of lowercase letters, return true if there is a cycle of length ≥ 4 made of same-letter cells, moving up, down, left or right, without immediately going back to the cell you just came from.

Related guides