Skip to content

Commit 12d1fc0

Browse files
committed
feat: Solve implement-trie-prefix-tree problem
1 parent 2e423e4 commit 12d1fc0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

implement-trie-prefix-tree/hu6r1s.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Trie:
2+
3+
def __init__(self):
4+
self.root = {"$": True}
5+
6+
def insert(self, word: str) -> None:
7+
node = self.root
8+
for ch in word:
9+
if ch not in node:
10+
node[ch] = {"$": False}
11+
node = node[ch]
12+
node["$"] = True
13+
14+
def search(self, word: str) -> bool:
15+
node = self.root
16+
for ch in word:
17+
if ch not in node:
18+
return False
19+
node = node[ch]
20+
return node["$"]
21+
22+
def startsWith(self, prefix: str) -> bool:
23+
node = self.root
24+
for ch in prefix:
25+
if ch not in node:
26+
return False
27+
node = node[ch]
28+
return True
29+
30+
31+
# Your Trie object will be instantiated and called as such:
32+
# obj = Trie()
33+
# obj.insert(word)
34+
# param_2 = obj.search(word)
35+
# param_3 = obj.startsWith(prefix)

0 commit comments

Comments
 (0)