forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWildcardSearchDS.kt
139 lines (120 loc) · 4.26 KB
/
WildcardSearchDS.kt
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
package questions
import _utils.UseCommentAsDocumentation
import kotlin.test.assertFalse
import kotlin.test.assertTrue
/**
* Design a data structure that supports adding new words and finding if a string matches any previously added string.
* Your data structure should implement two methods
* * `addWord(word)`- Adds word to the data structure
* * `searchWorld(word)`- Returns true if there is any string in the data structure that matches word. Word may contain dots where a dot can be matched with any letter (a dot represents a wildcard).
*/
@UseCommentAsDocumentation
class Trie {
var value: Char? = null
private val trieMap: MutableMap<Char, Trie> = mutableMapOf()
fun addWord(word: String) {
addWord(word, 0)
}
private fun addWord(word: String, index: Int) {
val letter = word.getOrNull(index) ?: return
if (letter !in trieMap) {
val newTrie = Trie()
trieMap[letter] = newTrie
newTrie.value = letter
newTrie.addWord(word, index + 1)
} else {
val trie = trieMap[letter]!!
trie.addWord(word, index + 1)
}
}
override fun toString(): String {
return "Trie($value, ${trieMap.values})"
}
private fun depthFirstTraversal(trie: Trie, current: String, word: MutableList<String>) {
if (trie.trieMap.isEmpty()) {
word.add(current)
}
trie.trieMap.forEach { (k, v) ->
depthFirstTraversal(v, current + k, word)
}
}
fun searchWord(word: String): Boolean {
return searchWord(word, 0)
}
private fun searchWord(word: String, index: Int): Boolean {
val letter = word.getOrNull(index) ?: return true
if (letter == '.') { // wildcard
// Ignore this and try to find any other node that matches next character
return trieMap.values.map {
it.searchWord(word, index + 1)
}.any { it }
} else {
val nextTrie = trieMap[letter] ?: return false
return nextTrie.searchWord(word, index + 1)
}
}
fun branches(): List<String> {
val words = mutableListOf<String>()
depthFirstTraversal(this, "", words)
println(words)
return words
}
}
fun main() {
run {
val trie = Trie()
trie.addWord("hello")
trie.addWord("word")
trie.addWord("woke")
trie.addWord("world")
assertTrue { trie.searchWord("hello") }
assertTrue { trie.searchWord("word") }
assertFalse { trie.searchWord("worry") }
assertTrue { trie.searchWord("wo.d") }
assertTrue { trie.searchWord("wor.d") }
trie.addWord("wore")
trie.addWord("wop")
trie.addWord("won")
trie.addWord("worst")
assertTrue { trie.searchWord("wor.t") }
assertFalse { trie.searchWord("wor.ts") }
assertFalse { trie.searchWord("worm") }
trie.addWord("worm")
assertTrue { trie.searchWord("worm") }
assertTrue { trie.searchWord("w.rm") }
assertTrue { trie.searchWord("w..m") }
assertTrue { trie.searchWord("...m") }
assertFalse { trie.searchWord("...ms") }
assertTrue { trie.searchWord("w...") }
}
run {
val trie = Trie()
trie.addWord("egg")
trie.addWord("eggplant")
trie.addWord("eggshell")
trie.addWord("elephant")
trie.addWord("eleanor")
trie.addWord("eleven")
trie.addWord("elegant")
trie.addWord("evil")
assertTrue { trie.searchWord("egg") }
assertTrue { trie.searchWord("eg.") }
assertTrue { trie.searchWord("eg.p.a.t") }
assertTrue { trie.searchWord("elep.a.t") }
assertFalse { trie.searchWord("elope") }
assertFalse { trie.searchWord("el.phat") }
assertFalse { trie.searchWord("elev.ns") }
assertFalse { trie.searchWord("elevens") }
assertFalse { trie.searchWord("eleventh") }
assertFalse { trie.searchWord("eleven.") }
trie.addWord("watch")
trie.addWord("witch")
trie.addWord("with")
trie.addWord("without")
trie.addWord("withe")
trie.addWord("wither")
trie.addWord("wit")
trie.addWord("withering")
trie.branches()
}
}