Skip to content

Commit 6c3f02d

Browse files
authored
Update longest-word-with-all-prefixes.py
1 parent b319003 commit 6c3f02d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Python/longest-word-with-all-prefixes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def longestWord(self, words):
1111
:type words: List[str]
1212
:rtype: str
1313
"""
14-
def iter_dfs(node):
14+
def iter_dfs(words, node):
1515
result = -1
1616
stk = [node]
1717
while stk:
@@ -29,7 +29,7 @@ def iter_dfs(node):
2929
trie["_end"] = -1
3030
for i, word in enumerate(words):
3131
reduce(dict.__getitem__, word, trie)["_end"] = i
32-
result = iter_dfs(trie)
32+
result = iter_dfs(words, trie)
3333
return words[result] if result != -1 else ""
3434

3535

@@ -45,19 +45,19 @@ def longestWord(self, words):
4545
:type words: List[str]
4646
:rtype: str
4747
"""
48-
def dfs(node, result):
48+
def dfs(words, node, result):
4949
if result[0] == -1 or len(words[node["_end"]]) > len(words[result[0]]):
5050
result[0] = node["_end"]
5151
for c in string.ascii_lowercase:
5252
if c not in node or "_end" not in node[c]:
5353
continue
54-
dfs(node[c], result)
54+
dfs(words, node[c], result)
5555

5656
_trie = lambda: collections.defaultdict(_trie)
5757
trie = _trie()
5858
trie["_end"] = -1
5959
for i, word in enumerate(words):
6060
reduce(dict.__getitem__, word, trie)["_end"] = i
6161
result = [-1]
62-
dfs(trie, result)
62+
dfs(words, trie, result)
6363
return words[result[0]] if result[0] != -1 else ""

0 commit comments

Comments
 (0)