Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 016e11b

Browse files
committed
created solution for problem70
1 parent be1d0d7 commit 016e11b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

problem70/Solution.py

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

0 commit comments

Comments
 (0)