FastPrepTrapped Characters in a Grid

Trapped Characters in a Grid

Adobe logoAdobe● EasyFULLTIMEOA
Learn

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 <= 200
  • 1 <= grid[i].length <= 200
  • Every row has the same length.
  • Each cell is X, an ASCII letter, or a decimal digit.

More Adobe problems

See Adobe hiring insights
public String[] trappedCharacters(String[] grid) {
    // Write your solution here
}
grid["XXXXX","XAX1X","XXXXX"]
expected["A 1 1", "1 1 3"]
Checking account…