File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -13,4 +13,4 @@ trie.contains(word: "a")
13
13
trie. contains ( word: " apple " )
14
14
15
15
trie. insert ( word: " apple " )
16
- trie. contains ( word: " apple " )
16
+ trie. contains ( word: " apple " )
Original file line number Diff line number Diff line change 4
4
/// -wordCount (the number of words in the trie)
5
5
public final class Trie {
6
6
typealias Node = TrieNode < Character >
7
+
8
+ public fileprivate( set) var words : Set < String > = [ ]
9
+
10
+ public var isEmpty : Bool {
11
+ return count == 0
12
+ }
13
+
14
+ public var count : Int {
15
+ return words. count
16
+ }
17
+
7
18
fileprivate let root : Node
8
19
9
20
public init ( ) {
@@ -14,7 +25,7 @@ public final class Trie {
14
25
// MARK: - Basic Methods
15
26
public extension Trie {
16
27
func insert( word: String ) {
17
- guard !word. isEmpty else { return }
28
+ guard !word. isEmpty, !contains ( word : word ) else { return }
18
29
var currentNode = root
19
30
20
31
var characters = Array ( word. lowercased ( ) . characters)
@@ -34,6 +45,8 @@ public extension Trie {
34
45
currentNode. isTerminating = true
35
46
}
36
47
}
48
+
49
+ words. insert ( word)
37
50
}
38
51
39
52
func contains( word: String ) -> Bool {
@@ -56,7 +69,9 @@ public extension Trie {
56
69
}
57
70
58
71
func remove( word: String ) {
59
- guard !word. isEmpty else { return }
72
+ guard !word. isEmpty, words. contains ( word) else { return }
73
+ words. remove ( word)
74
+
60
75
var currentNode = root
61
76
62
77
var characters = Array ( word. lowercased ( ) . characters)
You can’t perform that action at this time.
0 commit comments