-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordSearch.go
80 lines (66 loc) · 1.71 KB
/
wordSearch.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
package trie
/**
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example:
Input:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Output: ["eat","oath"]
Note:
You may assume that all inputs are consist of lowercase letters a-z.
*/
const mark = byte('#')
func dfs(board [][]byte, i int, j int, trie Trie, result *[]string, curr string) {
c := board[i][j]
_, ok := trie.Children[rune(c)]
if board[i][j] == mark || !ok {
return
}
trie = *trie.Children[rune(c)]
if trie.isTerminalWord {
*result = append(*result, curr + string(c))
}
board[i][j] = mark
if i > 0 {
dfs(board, i-1, j, trie, result, curr + string(c))
}
if j > 0 {
dfs(board, i, j-1, trie, result, curr + string(c))
}
if i < len(board) - 1 {
dfs(board, i+1, j, trie, result, curr + string(c))
}
if j < len(board[0]) - 1 {
dfs(board, i, j+1, trie, result, curr + string(c))
}
board[i][j] = c
}
func removeDuplicates(array []string) (result []string) {
seen := map[string]bool{}
for _, value := range array {
if seen[value] == false {
seen[value] = true
result = append(result, value)
}
}
return result
}
func FindWords(board [][]byte, words []string) []string {
trie := Constructor()
for _, word := range words {
trie.Insert(word)
}
result := make([]string, 0)
for i, row := range board {
for j := range row {
dfs(board, i, j, trie, &result, "")
}
}
return removeDuplicates(result)
}