Skip to content

Commit

Permalink
[DFS] Add a solution to Add and Search Word - Data structure design
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Apr 8, 2018
1 parent 3bbbf4e commit ea77b6b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
79 changes: 79 additions & 0 deletions DFS/WordDictionary.swift
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
}
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)|
Expand Down Expand Up @@ -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 |
Expand Down

0 comments on commit ea77b6b

Please sign in to comment.