Can a Word Be Traced Through Adjacent Board Letters?
Implement wordExistsOnBoard
Given a grid of letters and a target word, determine whether the word can be traced out by moving between horizontally or vertically adjacent cells, using each cell at most once along the path.
Computing the result of continuing in each of the four directions into its own variable before combining them with OR is correct, but it means every direction gets explored even after an earlier one has already confirmed the word can be completed from here. Chaining the same four calls together in a single OR expression relies on short-circuit evaluation — standard in every mainstream language — so the moment one direction succeeds, the rest are never called at all.
Example 1:
Input: board = ["HAT","ELP","SOS"], word = "HELP"
Output: true
Example 2:
Input: board = ["HAT","ELP","SOS"], word = "HATS"
Output: false
Example 3:
Input: board = ["HAT","ELP","SOS"], word = "SOS"
Output: true
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ board.length, board[0].length ≤ 6 - ●
1 ≤ word.length ≤ 15 - ●
board and word consist of uppercase English letters - ●
Each cell of the board may be used at most once per attempted path, and only horizontally/vertically adjacent cells may follow each other
board =
["HAT", "ELP", "SOS"]
word =
HELP