-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_search_ii.go
66 lines (62 loc) · 1.56 KB
/
word_search_ii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package trie
import "github.com/anirudhology/leetcode-go/problems/util"
func WordSearchII(board [][]byte, words []string) []string {
// List to store final output
var result []string
// Special case
if len(board) == 0 || len(words) == 0 {
return result
}
// Create trie with words
root := createTrie(words)
// Dimensions of the board
m, n := len(board), len(board[0])
// Process every cell on the board
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
dfs(board, i, j, m, n, root, &result)
}
}
return result
}
func createTrie(words []string) *util.TrieNodeWithWord {
root := &util.TrieNodeWithWord{}
for _, word := range words {
current := root
for _, c := range word {
if current.Children[int(c)-int('a')] == nil {
current.Children[int(c)-int('a')] = &util.TrieNodeWithWord{}
}
current = current.Children[int(c)-int('a')]
}
current.Word = word
}
return root
}
func dfs(board [][]byte, i int, j int, m int, n int, node *util.TrieNodeWithWord, result *[]string) {
// Handle out of bounds indices
if i < 0 || i >= m || j < 0 || j >= n {
return
}
c := board[i][j]
// If we have visited this cell already
if c == '#' {
return
}
index := int(c - 'a')
if node.Children[index] == nil {
return
}
node = node.Children[index]
if len(node.Word) > 0 {
*result = append(*result, node.Word)
node.Word = "" // Mark as visited
}
board[i][j] = '#'
// Perform DFS
dfs(board, i-1, j, m, n, node, result)
dfs(board, i+1, j, m, n, node, result)
dfs(board, i, j-1, m, n, node, result)
dfs(board, i, j+1, m, n, node, result)
board[i][j] = c
}