FastPrepCollect Letters Along a Unique Grid Path

Collect Letters Along a Unique Grid Path

Apple logoApple● EasyFULLTIMEOA
Learn

Problem statement

The 10-row, 15-column grid contains one path. Its cells are * or lowercase English letters. The path starts at (0, 0), moves horizontally or vertically, has no branches, and never forms a loop.

Follow the path from its start to its endpoint. Append each letter in encounter order and ignore stars. Return the resulting string.

For this exercise, unused cells contain .. All non-dot cells form that one path, and its starting cell is an endpoint. A one-cell path and a path containing no letters are allowed.

Function

collectPathLetters(grid: String[]) → String

Examples

Example 1

grid = ["*a*............","..b............","c**............","...............","...............","...............","...............","...............","...............","..............."]return = "abc"

The path moves right, down, and then left, encountering a, b, and c.

Example 2

grid = ["x..............","*..............","y..............","...............","...............","...............","...............","...............","...............","..............."]return = "xy"

The starting and ending cells may both contain letters.

Constraints

  • grid.length == 10.
  • Every row contains exactly 15 characters.
  • Each character is ., *, or a lowercase English letter.
  • The four-neighbor graph of non-dot cells is one simple path with endpoint (0, 0).

More Apple problems

See Apple hiring insights
public String collectPathLetters(String[] grid) {
    // Write your solution here.
}
grid["*a*............","..b............","c**............","...............","...............","...............","...............","...............","...............","..............."]
expected"abc"
Checking account…