Link: https://leetcode.com/problems/word-search-ii
Solution:
Intuition
This is a really good graph problem. The key here is to use a trie to traverse every valid path (word) in the board starting at every position. We do this because if the letter at a certain position is not in the top level of the trie then it cannot be a word (no word starts with that letter). We can use this logic recursively to make this decision at every level of our trie. If the path is valid, and the trie at that level contains the word delimiter then that word can be added to found_words
.
Once a word has been found, we don’t need to find it again so we can prune the word delimiter from our trie. Furthermore, if the trie at that level is empty, we can prune it further to prevent redundant searches.
The other consideration for the traversal is that we must mark nodes in the path as being visited such that no letter is used more than once (and to prevent stack overflow in certain cases).
Implementation
Visual
Review 1
Great problem! It was very easy this time around. Don’t forget to put the word itself in the trie under the #
key. Use a back tracking strategy to avoid cycles. Also, don’t forget to pop off the #
delimiter once a word has been found; this is so that we don’t look for this word again (you can imagine a board of all a
searching for aaa...
. ,finding it once is enough).
Also don’t forget to pop off trie[char]
if the hash-map is empty…otherwise there is no effect to popping the delimiter.