mediumBacktrackingMicrosoftFacebookBloomberg
Grid Letter Trailblazer
A route planner tries to spell a target word by walking neighboring tiles on a letter board without revisiting any cell.
Given a 2-D board and a word, return true if the word exists on the board. The word can be built from letters of sequentially adjacent cells.
Choose approach
A
cur
B
C
E
S
F
C
S
A
D
E
E
1/7
Narrationstep 1 of 7
Match 'A' at (0,0) with word[0].
Variables
r=0
c=0
i=0
char=A
path=[0,0]
How to think about it
- Match path letters sequentially; mismatch or OOB -> return false.
- Mark visited cells with '#'; restore after DFS returns.
- Short-circuit on first success.
Edge cases to catch in interview
- Empty word -> trivially true.
- Word longer than grid cells -> impossible.
Examples
Example 1
true{"grid":[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"word":"ABCCED"}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed