You're building a browser word game. The board is a grid of letter tiles, and a player forms a word by dragging through tiles that are horizontally or vertically adjacent, never using the same tile twice in one word.
Write canTrace(board, word), which returns true if word can be traced on board.
const board = [
['R', 'E', 'A', 'C'],
['S', 'T', 'X', 'T'],
['H', 'O', 'O', 'K'],
];
canTrace(board, 'REACT'); // true (R → E → A → C → T, down from C)
canTrace(board, 'HOOK'); // true
canTrace(board, 'SHOT'); // true (S → H → O → T, up from the first O)
canTrace(board, 'TEST'); // false (no S is adjacent to the E)
canTrace(board, 'STORE'); // false
canTrace(board, 'HOOH'); // false (would need the H tile twice)
word is non-empty. Don't mutate board (or, if you mark tiles temporarily, restore them before returning).Spotted a mistake or a rights concern? Report an issue