From 39262bfbe4b55a2934bba97a37fdd90d19617cec Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:24:41 -0700 Subject: [PATCH] add trie class --- tries/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tries/README.md b/tries/README.md index a922179..dbeeec7 100644 --- a/tries/README.md +++ b/tries/README.md @@ -18,6 +18,37 @@
+```python +class Trie: + + def __init__(self): + self.root = {} + + def insert(self, word: str) -> None: + node = self.root + for c in word: + if c not in node: + node[c] = {} + node = node[c] + node['$'] = None + + def match(self, seq, prefix=False): + node = self.root + for c in seq: + if c not in node: + return False + node = node[c] + return prefix or ('$' in node) + + def search(self, word: str) -> bool: + return self.match(word) + + def startsWith(self, prefix: str) -> bool: + return self.match(prefix, True) +``` + +
+ ---- ### insertion