Trapped Characters in a Grid
Problem statement
Given a rectangular character grid, a cell is a trapped character when all of the following hold:
- The cell is not
X. - It has four in-bounds orthogonal neighbors: up, down, left, and right.
- All four of those neighbors contain
X.
Return one string for every trapped character in row-major order. Format each string as character row column, using zero-based indices and single spaces.
Function
trappedCharacters(grid: String[]) → String[]Examples
Example 1
grid = ["XXXXX","XAX1X","XXXXX"]return = ["A 1 1","1 1 3"]Both interior non-X cells have X directly above, below, left, and right. Row-major order reports A first.
Example 2
grid = ["AXX","XXX","XXB"]return = []The two characters lie on the boundary and therefore do not have four in-bounds neighbors.
Constraints
1 <= grid.length <= 2001 <= grid[i].length <= 200- Every row has the same length.
- Each cell is
X, an ASCII letter, or a decimal digit.