Given a grid of letters, board, and a list of words, words, return every word in the list that can be traced in the grid. Each letter of a trace must be in a cell next to the last one: up, down, left or right. The same cell can't be used twice in one word, and the words can come back in any order.
Examples
Example 1:
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Example 2:
Input: board = [["a","b"],["c","d"]], words = ["ab","acb","abdc","abcd"]
Output: ["ab","abdc"]
Example 3:
Input: board = [["a"]], words = ["a","aa"]
Output: ["a"]
Example 4:
Input: board = [["a","a","a"],["a","a","a"],["a","a","a"]], words = ["aaaaaaaaa","aaaaaaaaaa"]
Output: ["aaaaaaaaa"]
Explanation: nine a's can snake through every cell once; a tenth needs a reuse.
Constraints
1 <= rows, columns <= 12
1 <= words.length <= 100
1 <= words[i].length <= 10
Board and words consist of lowercase English letters; words are distinct.
Prerequisites
Backtracking — choose, recurse, undo. The undo is what makes the rest correct. Pruning: cutting a branch the moment it cannot lead anywhere.
How to think about it
1. Trie Walk, Cutting Branches as You Go Optimal
Intuition
Put the words in a trie, and walk the board and the trie together. Each word's last node holds the word. A walk starts at every cell holding the root, and steps onto a neighbour only where the node it holds has a child for that letter, so a path no word begins with stops at once, and words that share a prefix share one walk. The cell is marked by writing a '#' into the board, which matches no letter, and its letter is put back on the way out. A word found is added and cleared from its node, so a second trace of it adds nothing. And on the way back, a node with no children left is cut from its parent: every word under it is found, so no later walk steps into it again.
Algorithm
1. Build the trie: each word goes a letter at a time from the root, making each missing child, and the last letter's node holds the word. 2. Keep the board and an empty answer, and call dfs(r, c, root) from every cell. 3. In dfs(r, c, parent): off the board, return. The parent has no child for the cell's letter (a marked cell never has one), return. 4. Step into that child. If it holds a word, add the word and clear it. 5. Mark the cell '#', and try down, up, right and left, each holding the child. 6. Put the cell's letter back. If the child has no children left, cut it from the parent. 7. Return the answer.
Time & Space
Time O(W) to build the trie, for W letters in all the words; then each cell of an r by c board starts a walk, a path is at most L letters long, and each step after the first has at most three new neighbours: O(r·c·3^L) at most. Cutting used-up branches only makes it faster. Space O(W) for the trie, and O(L) for the calls; the marks are in the board.
2. One Walk, Guided by a Trie Optimal
Intuition
Searching per word re-walks the board for every word, and words sharing a prefix re-walk the same cells. Put the words in a trie and walk the board ONCE, descending the trie as the path grows: shared prefixes are explored a single time, and a path dies the moment the trie has no child for the next letter — which is the pruning that makes a large word list tractable.
Algorithm
1. Insert every word into a trie, marking ends. 2. DFS from each cell, carrying the matching trie node. 3. A node marking a word end records that word. 4. Mark and unmark cells as in Word Search.
Time & Space
Time O(cells x 3^maxLen) for the single walk, independent of the number of words. Space O(total letters) for the trie.
3. One Word at a Time
Intuition
Run the single-word search once per word. At these board sizes it passes, and it is the answer to give before the clever one — an interviewer wants the working version on the board before the optimisation.
Algorithm
1. For each word, run the Word Search DFS. 2. Collect the ones that trace. 3. Sort the results.