Find a Same-Letter Loop in a Grid

Quick Overview

Find a Same-Letter Loop in a Grid. Given a rectangular lowercase-character grid, decide whether four-directional moves can form a cycle of at least four cells, all carrying the same character.

Find a Same-Letter Loop in a Grid

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given a rectangular lowercase-character `grid`, decide whether four-directional moves can form a cycle of at least four cells, all carrying the same character. The path returns to its starting cell, and a move may not immediately reverse the preceding move. Return true if such a cycle exists. ```cpp class Solution { public: bool containsCycle(vector<vector<char>>& grid); }; ``` ### Examples **Example 1:** ```text grid = [["q","q"],["q","q"]] Output: true Explanation: The four perimeter cells form a same-letter cycle. ``` **Example 2:** ```text grid = [["a","a","b"],["c","a","b"]] Output: false Explanation: Each equal-letter connected component is a path or a single cell. ``` ### Constraints - `m == grid.length` and `n == grid[i].length`. - `1 <= m,n <= 500`. - Every cell contains a lowercase English letter. ### Clarifying Questions Only up, down, left, and right moves count. Traversing one edge forward and backward does not form a valid cycle. ```hint Inspect the structure How will you distinguish a valid equal-letter loop from immediately retracing a single edge? ``` ### Reasoning checkpoints - Show that a detected loop obeys the equal-character, four-direction, and minimum-length rules. - Explain why the test neither accepts immediate edge reversal nor misses a valid cycle elsewhere in the grid. ### Optional extensions How would diagonal moves change the minimum possible cycle length?

Overview: Find a Same-Letter Loop in a Grid. Given a rectangular lowercase-character grid, decide whether four-directional moves can form a cycle of at least four cells, all carrying the same character.

Amazon logo
Amazon
Jun 6, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a rectangular lowercase-character grid, decide whether four-directional moves can form a cycle of at least four cells, all carrying the same character. The path returns to its starting cell, and a move may not immediately reverse the preceding move. Return true if such a cycle exists.

class Solution {
public:
    bool containsCycle(vector<vector<char>>& grid);
};

Examples

Example 1:

grid = [["q","q"],["q","q"]]
Output: true
Explanation: The four perimeter cells form a same-letter cycle.

Example 2:

grid = [["a","a","b"],["c","a","b"]]
Output: false
Explanation: Each equal-letter connected component is a path or a single cell.

Constraints

  • m == grid.length and n == grid[i].length .
  • 1 <= m,n <= 500 .
  • Every cell contains a lowercase English letter.

Clarifying Questions Guidance

Only up, down, left, and right moves count. Traversing one edge forward and backward does not form a valid cycle.

Reasoning checkpoints

  • Show that a detected loop obeys the equal-character, four-direction, and minimum-length rules.
  • Explain why the test neither accepts immediate edge reversal nor misses a valid cycle elsewhere in the grid.

Optional extensions

How would diagonal moves change the minimum possible cycle length?