Link: https://leetcode.com/problems/word-search/
Solution:
Topics: Word search II, topological order, DFS
Intuition
Cute little problem. Crushed it in about 3 minutes. Word search II is way more interesting. Time complexity is a bit weird. I was initially thinking about it in terms of if the length of the word was greater than the number of elements in the board…in that case complexity is o((n*m)*(n*m))
. But the constraints tell us that the length of the word is always smaller than the number of elements.
Basically in the DFS, for the first letter we branch off in 4 directions, and the depth of each direction is potentially len(word)
, so the complexity should be o((m*n)*(4**len(word)))
? Almost. In fact we only branch off in 4 directions for the first letter. All subsequent letters only branch in 3 directions because they cannot cross over their own path! So the complexity of each recursion is 3**len(word)
.
Implementation