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:
+ +Trie() Initializes the trie object.void insert(String word) Inserts the string word into the trie.boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.+
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:
+ +1 <= word.length, prefix.length <= 2000word and prefix consist only of lowercase English letters.3 * 104 calls in total will be made to insert, search, and startsWith.