Skip to content

Commit

Permalink
[Design] 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 Dec 8, 2019
1 parent d61910a commit cc7cdec
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
83 changes: 83 additions & 0 deletions Design/AddSearchWord.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
*/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 298 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 299 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -382,6 +382,7 @@
[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/| [Swift](./Design/DesignTicTacToe.swift)| Medium| O(1)| O(n)|
[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Swift](./Design/Vector2D.swift)| Medium | O(n)| O(n)|
[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)| [Swift](./Design/ImplementTrie.swift)| Medium | O(n)| O(n)|
[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Swift](./Design/AddSearchWord.swift)| Medium | O(24^n)| O(n)|
[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/)| [Swift](./Design/InsertDeleteGetRandom.swift)| Medium| O(1)| O(n)|
[LRU Cache](https://leetcode.com/problems/lru-cache/)| [Swift](./Design/LRUCache.swift)| Hard| O(1)| O(n)|

Expand Down

0 comments on commit cc7cdec

Please sign in to comment.