-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (125 loc) · 3.71 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import "fmt"
/*
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Example 2:
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
Constraints:
m == board.length
n == board[i].length
1 <= m, n <= 12
board[i][j] is a lowercase English letter.
1 <= words.length <= 3 * 104
1 <= words[i].length <= 10
words[i] consists of lowercase English letters.
All the strings of words are unique.
*/
type TrieNode struct {
isEndOfWord bool
children map[rune]TrieNode
}
func DefaultTrieNode() TrieNode {
return TrieNode{
isEndOfWord: false,
children: make(map[rune]TrieNode),
}
}
func DefaultTrie() *TrieNode {
return &TrieNode{isEndOfWord: false, children: make(map[rune]TrieNode)}
}
func (trie *TrieNode) Insert(word string) *TrieNode {
currentNode := *trie
for index, letter := range word {
if _, ok := currentNode.children[letter]; !ok {
node := DefaultTrieNode()
node.isEndOfWord = index == len(word)-1
currentNode.children[letter] = node
}
currentNode = currentNode.children[letter]
}
return trie
}
func (trie *TrieNode) Contains(word string) bool {
currentNode := *trie
for _, letter := range word {
if _, ok := currentNode.children[letter]; !ok {
return false
}
currentNode = currentNode.children[letter]
}
return currentNode.isEndOfWord
}
func (trie *TrieNode) NodeContainsCharacter(character rune) bool {
_, ok := trie.children[character]
return ok
}
func keys(m map[string]bool) []string {
out := make([]string, 0, len(m))
for key := range m {
out = append(out, key)
}
return out
}
// time O(rows * columns * 4 ^ rows * colums)
// 4 because there are 4 directions we can to go in theboard
// 4 ^ rows * columns is how long a dfs can take
// rows * columns because we are doing it starting from each row and column
func findWords(board [][]rune, words []string) []string {
type position struct {
i int
j int
}
trie := DefaultTrie()
for _, word := range words {
trie.Insert(word)
}
wordsThatCanBeBuiltFromBoard := make(map[string]bool)
visitedPositions := make(map[position]bool)
var dfs func(i int, j int, currentWord string, currentNode TrieNode)
rows := len(board)
columns := len(board[0])
dfs = func(i int, j int, currentWord string, currentNode TrieNode) {
if i < 0 || i >= rows || j < 0 || j >= columns {
return
}
currentCharacter := board[i][j]
if !currentNode.NodeContainsCharacter(currentCharacter) {
return
}
currentNode = currentNode.children[currentCharacter]
currentWord += string(currentCharacter)
currentPosition := position{i, j}
visitedPositions[currentPosition] = true
if currentNode.isEndOfWord {
wordsThatCanBeBuiltFromBoard[currentWord] = true
}
dfs(i-1, j, currentWord, currentNode)
dfs(i+1, j, currentWord, currentNode)
dfs(i, j-1, currentWord, currentNode)
dfs(i, j+1, currentWord, currentNode)
visitedPositions[currentPosition] = false
}
for i := 0; i < rows; i++ {
for j := 0; j < columns; j++ {
dfs(i, j, "", *trie)
}
}
return keys(wordsThatCanBeBuiltFromBoard)
}
func main() {
trie := DefaultTrie().
Insert("hello").
Insert("world").
Insert("one").
Insert("two").
Insert("three")
fmt.Println(trie.Contains("hello"))
fmt.Println(trie.Contains("hel"))
fmt.Println(trie.Contains("world"))
fmt.Println(trie.Contains("hello"))
}