|
| 1 | +""" |
| 2 | +Iterate through the board. |
| 3 | +When we spot a starting point, we `dfs()` to see if it can finish the word. |
| 4 | +To prevent travel to the visited node, we use a hash-set `set()` to keep track of what we have visited. |
| 5 | +`l` is the index of the character in the `word` we are checking. |
| 6 | +Note that we need to use `visited.copy()` to copy a whole new hash-set, or all the `dfs()` will use the same hash-set, which is not what we want. |
| 7 | +
|
| 8 | +The time complexity is `O(N^2)`, `N` is the number of nodes. You can think of it as for each node, we have to decided to go ot not to go, and we need to make the decision `MN` times. |
| 9 | +The space complexity is `O(W*N)`, `W` is the length of `word`. Because the recursion level is `W`, and for each recursion we need to keep track of `visited`, which potensially takes `O(N)`. |
| 10 | +""" |
| 11 | +class Solution(object): |
| 12 | + def exist(self, board, word): |
| 13 | + def getNeighbor(i, j): |
| 14 | + opt = [] |
| 15 | + if i+1<M: opt.append((i+1, j)) |
| 16 | + if j+1<N: opt.append((i, j+1)) |
| 17 | + if i-1>=0: opt.append((i-1, j)) |
| 18 | + if j-1>=0: opt.append((i, j-1)) |
| 19 | + return opt |
| 20 | + |
| 21 | + def dfs(i, j, l, visited): |
| 22 | + if l==len(word)-1: return True |
| 23 | + visited.add((i, j)) |
| 24 | + for ni, nj in getNeighbor(i, j): |
| 25 | + if (ni, nj) not in visited and board[ni][nj]==word[l+1]: |
| 26 | + if dfs(ni, nj, l+1, visited.copy()): return True |
| 27 | + return False |
| 28 | + |
| 29 | + M = len(board) |
| 30 | + N = len(board[0]) |
| 31 | + |
| 32 | + for i in xrange(M): |
| 33 | + for j in xrange(N): |
| 34 | + if board[i][j]==word[0]: |
| 35 | + if dfs(i, j, 0, set()): return True |
| 36 | + |
| 37 | + return False |
| 38 | + |
| 39 | +""" |
| 40 | +As you see, the above solution takes lots of space. |
| 41 | +Another way is to change the character on the `board` to `#` to represent it as visited. |
| 42 | +After the `dfs()` call we need to change it back. Because other branch of `dfs()` might not need visit this node, or haven't visit this node. |
| 43 | +
|
| 44 | +The time complexity is `O(N^2)`, `N` is the number of nodes. |
| 45 | +The space complexity is `O(W)`, `W` is the length of `word`. Because the recursion level is `W`. |
| 46 | +This answer is inspired by @caikehe's [solution](https://leetcode.com/problems/word-search/discuss/27660/). |
| 47 | +""" |
| 48 | +class Solution(object): |
| 49 | + def exist(self, board, word): |
| 50 | + def getNeighbor(i, j): |
| 51 | + opt = [] |
| 52 | + if i+1<M: opt.append((i+1, j)) |
| 53 | + if j+1<N: opt.append((i, j+1)) |
| 54 | + if i-1>=0: opt.append((i-1, j)) |
| 55 | + if j-1>=0: opt.append((i, j-1)) |
| 56 | + return opt |
| 57 | + |
| 58 | + def dfs(i, j, l): |
| 59 | + if l==len(word)-1 and board[i][j]==word[l]: return True |
| 60 | + if board[i][j]!=word[l]: return False |
| 61 | + |
| 62 | + char = board[i][j] |
| 63 | + board[i][j] = '#' |
| 64 | + |
| 65 | + for ni, nj in getNeighbor(i, j): |
| 66 | + if dfs(ni, nj, l+1): return True |
| 67 | + |
| 68 | + board[i][j] = char |
| 69 | + return False |
| 70 | + |
| 71 | + M = len(board) |
| 72 | + N = len(board[0]) |
| 73 | + |
| 74 | + for i in xrange(M): |
| 75 | + for j in xrange(N): |
| 76 | + if dfs(i, j, 0): return True |
| 77 | + |
| 78 | + return False |
0 commit comments