diff --git a/Solution/208. Implement Trie (Prefix Tree)/208. Implement Trie (Prefix Tree).py b/Solution/208. Implement Trie (Prefix Tree)/208. Implement Trie (Prefix Tree).py new file mode 100644 index 0000000..286796c --- /dev/null +++ b/Solution/208. Implement Trie (Prefix Tree)/208. Implement Trie (Prefix Tree).py @@ -0,0 +1,37 @@ +class Trie: + def __init__(self): + self.children = [None] * 26 + self.is_end = False + + def insert(self, word: str) -> None: + node = self + for c in word: + idx = ord(c) - ord('a') + if node.children[idx] is None: + node.children[idx] = Trie() + node = node.children[idx] + node.is_end = True + + def search(self, word: str) -> bool: + node = self._search_prefix(word) + return node is not None and node.is_end + + def startsWith(self, prefix: str) -> bool: + node = self._search_prefix(prefix) + return node is not None + + def _search_prefix(self, prefix: str): + node = self + for c in prefix: + idx = ord(c) - ord('a') + if node.children[idx] is None: + return None + node = node.children[idx] + return node + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) \ No newline at end of file diff --git a/Solution/208. Implement Trie (Prefix Tree)/readme.md b/Solution/208. Implement Trie (Prefix Tree)/readme.md new file mode 100644 index 0000000..399d79b --- /dev/null +++ b/Solution/208. Implement Trie (Prefix Tree)/readme.md @@ -0,0 +1,225 @@ +--- +comments: true +difficulty: Medium +edit_url: Antim +tags: + - Design + - Trie + - Hash Table + - String +--- + + + +# [208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) + + +## Description + + + +

A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

+ +

Implement the Trie class:

+ + + +

 

+

Example 1:

+ +
+Input
+["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
+[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
+Output
+[null, null, true, false, true, null, true]
+
+Explanation
+Trie trie = new Trie();
+trie.insert("apple");
+trie.search("apple");   // return True
+trie.search("app");     // return False
+trie.startsWith("app"); // return True
+trie.insert("app");
+trie.search("app");     // return True
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python +class Trie: + def __init__(self): + self.children = [None] * 26 + self.is_end = False + + def insert(self, word: str) -> None: + node = self + for c in word: + idx = ord(c) - ord('a') + if node.children[idx] is None: + node.children[idx] = Trie() + node = node.children[idx] + node.is_end = True + + def search(self, word: str) -> bool: + node = self._search_prefix(word) + return node is not None and node.is_end + + def startsWith(self, prefix: str) -> bool: + node = self._search_prefix(prefix) + return node is not None + + def _search_prefix(self, prefix: str): + node = self + for c in prefix: + idx = ord(c) - ord('a') + if node.children[idx] is None: + return None + node = node.children[idx] + return node + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) +``` + +#### Java + +```java +class Trie { + private Trie[] children; + private boolean isEnd; + + public Trie() { + children = new Trie[26]; + } + + public void insert(String word) { + Trie node = this; + for (char c : word.toCharArray()) { + int idx = c - 'a'; + if (node.children[idx] == null) { + node.children[idx] = new Trie(); + } + node = node.children[idx]; + } + node.isEnd = true; + } + + public boolean search(String word) { + Trie node = searchPrefix(word); + return node != null && node.isEnd; + } + + public boolean startsWith(String prefix) { + Trie node = searchPrefix(prefix); + return node != null; + } + + private Trie searchPrefix(String s) { + Trie node = this; + for (char c : s.toCharArray()) { + int idx = c - 'a'; + if (node.children[idx] == null) { + return null; + } + node = node.children[idx]; + } + return node; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = new Trie(); + * obj.insert(word); + * boolean param_2 = obj.search(word); + * boolean param_3 = obj.startsWith(prefix); + */ +``` + +#### C++ + +```cpp +class Trie { +private: + vector children; + bool isEnd; + + Trie* searchPrefix(string s) { + Trie* node = this; + for (char c : s) { + int idx = c - 'a'; + if (!node->children[idx]) return nullptr; + node = node->children[idx]; + } + return node; + } + +public: + Trie() + : children(26) + , isEnd(false) {} + + void insert(string word) { + Trie* node = this; + for (char c : word) { + int idx = c - 'a'; + if (!node->children[idx]) node->children[idx] = new Trie(); + node = node->children[idx]; + } + node->isEnd = true; + } + + bool search(string word) { + Trie* node = searchPrefix(word); + return node != nullptr && node->isEnd; + } + + bool startsWith(string prefix) { + Trie* node = searchPrefix(prefix); + return node != nullptr; + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ +``` + + + + + + \ No newline at end of file