File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,22 +15,27 @@ def __init__(self):
1515 self .end = False
1616 self .children = {}
1717
18- def add (self , word : str ):
19- cur = self
20- for ch in word :
21- child = cur .children .get (ch )
22- if not child :
23- child = Trie .Node ()
24- cur .children [ch ] = child
25- cur = child
26- cur .end = True
27-
2818 def __init__ (self , words : Iterable [str ]):
2919 self .words = words
3020 self .root = Trie .Node ()
3121
3222 for word in words :
33- self .root .add (word )
23+ self .add_word (word )
24+
25+ def add_word (self , word : str ) -> None :
26+ """
27+ Add a word to the trie.
28+
29+ :param str text: a word
30+ """
31+ cur = self .root
32+ for ch in word :
33+ child = cur .children .get (ch )
34+ if not child :
35+ child = Trie .Node ()
36+ cur .children [ch ] = child
37+ cur = child
38+ cur .end = True
3439
3540 def prefixes (self , text : str ) -> List [str ]:
3641 """
You can’t perform that action at this time.
0 commit comments