Find Every Dictionary Word Traceable on a Letter Board
Implement findBoardWords
You're given a letter board and a list of candidate words. Find every word from the list that can be traced through horizontally or vertically adjacent cells, using each cell at most once per word.
Searching the board separately and completely for each word finds every match, but words sharing a common prefix pay for that shared prefix's board steps over and over — once per word. Inserting every word into a trie first, then walking the board exactly once while moving through the trie in lockstep, means a shared prefix is only ever traversed a single time: the DFS reaching a trie node marked as a complete word records it and simply continues from that same node, since it might also be partway through a longer word.
Example 1:
Input: board = ["HAT","ELP","SOS"], words = ["HELP","HAT","SOS","SOLE","TAP"]
Output: ["HAT","HELP","SOLE","SOS"]
Example 2:
Input: board = ["HAT","ELP","SOS"], words = ["CAT","PEA","LEAP","HOLE"]
Output: []
Example 3:
Input: board = ["HAT","ELP","SOS"], words = ["AT"]
Output: ["AT"]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ board.length, board[0].length ≤ 6 - ●
0 ≤ words.length ≤ 12, each word 1–10 uppercase letters - ●
Each cell of the board may be used at most once per attempted word, and only horizontally/vertically adjacent cells may follow each other - ●
Results are returned sorted in ascending (lexicographic) order for a stable, checkable answer
board =
["HAT", "ELP", "SOS"]
words =
["HELP", "HAT", "SOS", "SOLE", "TAP"]