Time: O(N * 4^(L (len of word))) Space: O(L) recursion stack at most L
(board [][]byte, word string)
| 7 | // Time: O(N * 4^(L (len of word))) |
| 8 | // Space: O(L) recursion stack at most L |
| 9 | func exist(board [][]byte, word string) bool { |
| 10 | for r := 0; r < len(board); r++ { |
| 11 | for c := 0; c < len(board[r]); c++ { |
| 12 | if word[0] == board[r][c] && explore(board, word, 0, r, c) { |
| 13 | return true |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return false |
| 19 | } |
| 20 | |
| 21 | func explore(board [][]byte, word string, step, r, c int) bool { |
| 22 | // if the step is equal to the length of word, then word exists |