Notice Period

Badge Trail in the Grid

medium

A warehouse floor is a grid of shelving units, each labeled with a single letter. A security audit asks: can a guard walk a path that spells a given badge code?

Given a 2D grid board of single-character strings and a string code, return True if code can be traced by starting at any cell and repeatedly stepping to an adjacent cell (up, down, left, right). The same cell may not be used twice in one trace.

Example 1
in board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], code = "ABCCED"
out True
Start at the top-left A and snake right, then down: A→B→C→C→E→D.
Example 2
in same board, code = "ABCB"
out False
The trace would need to revisit the B it already used.
Constraints
  • · 1 <= rows, cols <= 6
  • · 1 <= len(code) <= 12
  • · board cells and code are uppercase or lowercase English letters
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.