From ea77b6b865471f9dce346da9e994421ebc81c6dc Mon Sep 17 00:00:00 2001 From: Yi Gu Date: Sun, 8 Apr 2018 15:34:59 +0800 Subject: [PATCH] [DFS] Add a solution to Add and Search Word - Data structure design --- DFS/WordDictionary.swift | 79 ++++++++++++++++++++++++++++++++++++++++ README.md | 5 ++- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 DFS/WordDictionary.swift diff --git a/DFS/WordDictionary.swift b/DFS/WordDictionary.swift new file mode 100644 index 00000000..cc1506d7 --- /dev/null +++ b/DFS/WordDictionary.swift @@ -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 + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 16ed587e..14ccc8ee 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ * [Microsoft](#microsoft) ## Progress -[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 230 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys). +[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 231 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys). ## Array @@ -215,6 +215,7 @@ [Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Swift](./DFS/WallsGates.swift)| Medium| O(n!)| O(2^n)| [Word Search](https://leetcode.com/problems/word-search/)| [Swift](./DFS/WordSearch.swift)| Medium| O((n^2)!)| O(n^2)| [Word Search II](https://leetcode.com/problems/word-search-ii/)| [Swift](./DFS/WordSearchII.swift)| Hard| O(((mn)^2))| O(n^2)| +[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Swift](./DFS/WordDictionary.swift)| Medium| O(n)| O(n)| [N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n^4))| O(n^2)| [N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n^3))| O(n)| [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)| [Swift](./DFS/SudokuSolver.swift)| Hard| O(n^4)| O(1)| @@ -587,7 +588,7 @@ | | 214 | [Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/) | Hard | | [Swift](./DP/HouseRobberII.swift) | 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | Medium | | [Swift](./DFS/WordSearchII.swift) | 212 | [Word Search II](https://leetcode.com/problems/word-search-ii/) | Hard | -| | 211 | [Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | Medium | +| [Swift](./DFS/WordDictionary.swift) | 211 | [Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | Medium | | | 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | Medium | | [Swift](./Array/MinimumSizeSubarraySum.swift) | 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | Medium | | | 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | Medium |