You are given a 2D grid of characters. Starting from position (0, 0), follow the unique path formed by '*' (star) characters and lowercase letters. At each step, move to an adjacent cell (up, down, left, right) that is part of the path and has not been visited yet.
Collect all the lowercase letters encountered along the path in order and return the resulting string.
Empty cells are represented by '.'. The cell at (0, 0) is always part of the path. The path is guaranteed to be unique (no branching), meaning at each step there is at most one unvisited neighbor that continues the path.
Function Signature
def collect_path_letters(grid: list[list[str]]) -> str:
Rules
-
grid
is a list of rows, and each row is a list of one-character strings.
-
Path cells are
'*'
and lowercase letters. A
'.'
cell is never entered.
-
The walk ends when the current cell has no unvisited path neighbor inside the grid.
-
A letter on
(0, 0)
counts. If the path holds no letters, return the empty string
""
.
Constraints
-
1 <= grid.length, grid[0].length <= 100
-
Each cell is either
'.'
,
'*'
, or a lowercase English letter.
-
The path starting from
(0, 0)
is unique (no branching).
-
The cell
(0, 0)
is always part of the path.
Examples
Input: grid = [["*","*",".","."],[".","h",".","."],[".","i","*","."],[".",".","*","."]]
Output: "hi"
The path is (0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2) -> (3,2). It meets 'h' at (1,1) and then 'i' at (2,1).
Input: grid = [["a","*","b"],[".",".","*"],[".",".","c"]]
Output: "abc"
The path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2). The start cell itself holds 'a', followed by 'b' at (0,2) and 'c' at (2,2).
Input: grid = [["*",".","a"],["*",".","b"],["*","*","*"]]
Output: "ba"
The path goes down the left column, along the bottom row and up the right column, so it meets 'b' at (1,2) before 'a' at (0,2).