File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ class TrieNode :
2
+ def __init__ (self ):
3
+ self .children = {}
4
+ self .is_word = False
5
+
6
+ class Trie :
7
+
8
+ def __init__ (self ):
9
+ self .root = TrieNode ()
10
+
11
+ def insert (self , word : str ) -> None :
12
+ node = self .root
13
+ for char in word :
14
+ if char not in node .children :
15
+ node .children [char ] = TrieNode ()
16
+ node = node .children [char ]
17
+ node .is_word = True
18
+
19
+ def search (self , word : str ) -> bool :
20
+ node = self .root
21
+ for char in word :
22
+ if char not in node .children :
23
+ return False
24
+ node = node .children [char ]
25
+ return node .is_word
26
+
27
+
28
+ def startsWith (self , prefix : str ) -> bool :
29
+ node = self .root
30
+ for char in prefix :
31
+ if char not in node .children :
32
+ return False
33
+ node = node .children [char ]
34
+ return True
You can’t perform that action at this time.
0 commit comments