-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00212-word_search_ii.go
76 lines (60 loc) · 1.62 KB
/
00212-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
// 212: Word Search Ii
// https://leetcode.com/problems/word-search-ii/
package main
import (
"fmt"
)
type TrieNode struct{
val rune
word string
next map[rune]*TrieNode
}
func dfs(board [][]byte, res *[]string, node *TrieNode, i,j int, visited map[string]bool){
k := fmt.Sprintf("%v_%v", i,j)
if i<0 || j<0 || i>=len(board) || j>=len(board[0]) || visited[k] { return }
nn, ok:= node.next[rune(board[i][j])]
if !ok {return}
if len(nn.word)>0 {
*res = append(*res, nn.word)
nn.word = ""
}
visited[k] = true
dfs(board, res, nn, i+1, j, visited)
dfs(board, res, nn, i-1, j, visited)
dfs(board, res, nn, i, j+1, visited)
dfs(board, res, nn, i, j-1, visited)
visited[k] = false
}
// SOLUTION
func findWords(board [][]byte, words []string) []string {
node := &TrieNode{next : map[rune]*TrieNode{}}
for _, w := range words {
p := node
for i, b := range w {
if _, ok := p.next[b]; !ok {
p.next[b] = &TrieNode{val:b, next:map[rune]*TrieNode{}}
}
if i==len(w)-1 {p.next[b].word = w}
p = p.next[b]
}
}
result := []string{}
for i:=0; i<len(board); i++ {
for j:=0; j<len(board[i]); j++ {
dfs(board, &result, node, i,j, map[string]bool{})
}
}
return result
}
func main() {
// INPUT
board := [][]byte{
{'o','a','a','n'},
{'e','t','a','e'},
{'i','h','k','r'},
{'i','f','l','v'},
}
words := []string{"oath","pea","eat","rain"}
// OUTPUT
fmt.Println(findWords(board, words))
}