forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
word-search-ii.go
executable file
·81 lines (65 loc) · 1.26 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package problem0212
func findWords(board [][]byte, words []string) []string {
var results []string
m := len(board)
if m == 0 {
return results
}
n := len(board[0])
if n == 0 {
return results
}
trie := buildTrie(words)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
dfs(board, i, j, trie, &results)
}
}
return results
}
func dfs(board [][]byte, i int, j int, trie *TrieNode, results *[]string) {
c := board[i][j]
if c == '#' || trie.next[int(c-'a')] == nil {
return
}
trie = trie.next[int(c-'a')]
if len(trie.word) > 0 {
// Found one
*results = append(*results, trie.word)
trie.word = ""
}
board[i][j] = '#'
if i > 0 {
dfs(board, i-1, j, trie, results)
}
if i < len(board)-1 {
dfs(board, i+1, j, trie, results)
}
if j > 0 {
dfs(board, i, j-1, trie, results)
}
if j < len(board[0])-1 {
dfs(board, i, j+1, trie, results)
}
board[i][j] = c
}
func buildTrie(words []string) *TrieNode {
root := new(TrieNode)
for _, word := range words {
cur := root
for _, c := range word {
cidx := int(c - 'a')
if cur.next[cidx] == nil {
cur.next[cidx] = new(TrieNode)
}
cur = cur.next[cidx]
}
cur.word = word
}
return root
}
// TrieNode 是 trie 的节点
type TrieNode struct {
next [26]*TrieNode
word string
}