You kind of have to go Trie plus DFS here.
Model the board as a graph and use a Trie to store the word list for efficient prefix pruning. Perform DFS from each cell, marking visited cells and backtracking, while checking the Trie to collect valid words. Optimize by stopping early when no words share the current prefix.
Pro tip: Mention that you can further optimize by removing matched words from the Trie to avoid duplicate searches, and discuss trade-offs between time and space complexity. Also, clarify assumptions about input size and character set.
Ask about board dimensions, word list size, character set, and whether words can be reused. Confirm that paths can start and end anywhere and that each cell can be used at most once per word.
Use a Trie to store the dictionary for O(1) prefix lookups and pruning. Use a 2D boolean array or modify the board in-place to track visited cells during DFS.
For each cell, start a DFS that explores all four directions, checking if the current path forms a prefix in the Trie. If a complete word is found, add it to the result set.
Mark the current cell as visited before recursing and unmark it after. Prune the search when the current prefix is not in the Trie. Optionally, remove found words from the Trie to avoid duplicates.
Discuss time complexity O(M * N * 4^L) where L is max word length, and space complexity O(total characters in words). Handle edge cases like empty board, empty word list, and single-character words.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.