-
Notifications
You must be signed in to change notification settings - Fork 886
/
AddSearchWord.swift
83 lines (68 loc) · 2.14 KB
/
AddSearchWord.swift
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
/**
* Question Link: https://leetcode.com/problems/add-and-search-word-data-structure-design/
* Primary idea: Use trie to add and search word. For '.' case we iterate through all sub nodes.
*
* Time Complexity: addWord - O(n), n stands for the length of the word; search - O(24^n)
* Space Complexity: addWord - O(n), search - O(1)
*
*/
class WordDictionary {
var head: TrieNode
/** Initialize your data structure here. */
init() {
head = TrieNode()
}
/** Adds a word into the data structure. */
func addWord(_ word: String) {
var node = head
for char in word {
if node.children[char] == nil {
node.children[char] = TrieNode()
}
node = node.children[char]!
}
node.isEnd = true
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
func search(_ word: String) -> Bool {
return search(word, head)
}
private func search(_ word: String, _ startNode: TrieNode) -> Bool {
var node = startNode
guard let char = word.first else {
return node.isEnd
}
if char != "." {
if node.children[char] == nil {
return false
} else {
return search(String(word.dropFirst()), node.children[char]!)
}
} else {
if node.children.isEmpty {
return false
} else {
for childNode in node.children.values {
if search(String(word.dropFirst()), childNode) {
return true
}
}
return false
}
}
}
}
class TrieNode {
var isEnd: Bool
var children: [Character: TrieNode]
init() {
isEnd = false
children = [Character: TrieNode]()
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* let obj = WordDictionary()
* obj.addWord(word)
* let ret_2: Bool = obj.search(word)
*/