forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DFS] Add a solution to Add and Search Word - Data structure design
- Loading branch information
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/add-and-search-word-data-structure-design/ | ||
* Primary idea: Trie with DFS to resolve search problem | ||
* | ||
* Time Complexity: O(n), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class WordDictionary { | ||
var trie = Trie() | ||
|
||
func add(word: String) { | ||
trie.add(word: word) | ||
} | ||
|
||
func search(word: String) -> Bool { | ||
return trie.search(word:word) | ||
} | ||
} | ||
|
||
class TrieNode { | ||
var children: [Character: TrieNode] | ||
var isEnd: Bool | ||
|
||
init() { | ||
self.children = [Character: TrieNode]() | ||
self.isEnd = false | ||
} | ||
} | ||
|
||
class Trie { | ||
var root: TrieNode | ||
|
||
init() { | ||
root = TrieNode() | ||
} | ||
|
||
func add(word: String) { | ||
var node = root | ||
|
||
for char in word { | ||
if node.children[char] == nil { | ||
node.children[char] = TrieNode() | ||
} | ||
|
||
node = node.children[char]! | ||
} | ||
|
||
node.isEnd = true | ||
} | ||
|
||
func search(word:String) -> Bool { | ||
return dfsSearch(word: word, index: 0, node: root) | ||
} | ||
|
||
fileprivate func dfsSearch(word: String, index: Int, node: TrieNode) -> Bool { | ||
if index == word.count { | ||
return node.isEnd | ||
} | ||
|
||
let char = Array(word)[index] | ||
|
||
if char != "." { | ||
guard let nextNode = node.children[char] else { | ||
return false | ||
} | ||
|
||
return dfsSearch(word: word, index: index + 1, node: nextNode) | ||
} else{ | ||
for key in node.children.keys { | ||
if dfsSearch(word: word, index: index + 1, node: node.children[key]!) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters