Café Letter Board
A café has a wall board of letter tiles in a grid. On quiz night, teams answer by tracing their word: start on any tile, then slide to the tile directly above, below, left or right for each next letter. Diagonal moves are not allowed, and a word cannot use the same tile twice.
The quiz host needs a quick check that an answer can really be traced on tonight's board.
Your task
Write canTrace(board, word). board is a list of equal-length strings, one per row, so board[r][c] is the letter in row r, column c. Return true if word can be traced as described, and false otherwise.
Consecutive letters must be on tiles that share a side, and each tile can be used at most once per word.
canTrace(board: string[], word: string) → bool
Examples
board = ["CAT", "ORE", "WEB"] word = "CORE"
true
The board isC A TO R EW E B
Start on C (top left), then down to O, right to R, right to E.
board = ["CAT", "ORE", "WEB"] word = "BEER"
false
B touches both Es, but the two Es (row 1, column 2 and row 2, column 1) touch only diagonally, so you can never step from one to the other.
board = ["CAT", "ORE", "WEB"] word = "RARE"
false
Spelling RARE would need to use the only R tile twice.
Constraints
- 1 ≤ number of rows, number of columns ≤ 6
- 1 ≤ word.length ≤ 15
- board and word contain only uppercase letters A to Z.
Start with brute force
From every tile, follow every walk of the word's length (up to four choices per step) and compare letters at the end. On a 6 × 6 board with a 15-letter word that is 36 × 414, around 9.7 billion walks.
Almost all go wrong by the second or third letter. Check each letter as you step on it and turn back at once on a mismatch.
Hints
0 of 4 shown- Hint 1
On the example board, CORE can only start on the C. Standing on C, where can the O be?
- Hint 2
The next letter must be on one of at most four side neighbours, and the one after must neighbour that tile. A path fails the moment a letter is wrong; then you go back and try another neighbour.
- Hint 3
Write
trace(r, c, k): can the word from letterkon be traced starting at tile (r, c)? It fails if the tile is off the board, already used, or the wrong letter. It succeeds ifkis the last letter. Otherwise ask the four neighbours withk + 1. - Hint 4
Mark the tile used before asking the neighbours and unmark it afterwards, whatever they answered. Call
trace(r, c, 0)from every tile and returntrueif any call does.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Depth-first search with backtracking follows one path as far as the letters match, and steps back to try another neighbour the moment one fails.
Trace CORE on the example board:
- Tiles other than C fail at once on the first letter. At C (row 0, column 0), mark the tile as used and look for an O among its neighbours.
- The tile to the right is an A, which is the wrong letter. The tile below is an O, which matches, so mark it and look for an R.
- From the O, the tile above is the C, which is already used, and the tile below is a W, which is wrong. The tile to the right is an R, which matches, so mark it and look for an E.
- From the R, the tile above is an A, which is wrong. The tile below is an E (row 2, column 1), and since it is the last letter, the search returns
trueall the way back up.
The unmark step matters. On a board like SEA / ETT / ESA with the word TASTE, a first attempt can mark a T, an A and an S and then fail. If they stay marked, they block the correct path, and you wrongly return false.
function canTrace(board, word) {
const rows = board.length, cols = board[0].length;
const used = board.map(() => new Array(cols).fill(false));
// can word[k..] be traced starting at tile (r, c)?
function trace(r, c, k) {
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (used[r][c] || board[r][c] !== word[k]) return false;
if (k === word.length - 1) return true; // last letter matched
used[r][c] = true;
const found = trace(r - 1, c, k + 1) || trace(r + 1, c, k + 1) ||
trace(r, c - 1, k + 1) || trace(r, c + 1, k + 1);
used[r][c] = false; // free the tile for other paths
return found;
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (trace(r, c, 0)) return true;
}
}
return false;
}def can_trace(board, word):
rows, cols = len(board), len(board[0])
used = [[False] * cols for _ in range(rows)]
def trace(r, c, k):
# can word[k:] be traced starting at tile (r, c)?
if r < 0 or c < 0 or r >= rows or c >= cols:
return False
if used[r][c] or board[r][c] != word[k]:
return False
if k == len(word) - 1:
return True # last letter matched
used[r][c] = True
found = (trace(r - 1, c, k + 1) or trace(r + 1, c, k + 1) or
trace(r, c - 1, k + 1) or trace(r, c + 1, k + 1))
used[r][c] = False # free the tile for other paths
return found
return any(trace(r, c, 0) for r in range(rows) for c in range(cols))Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.