Skip to content

Commit 79d812e

Browse files
committed
update longest-word-in-dictionary.py
a bit more pythonic way
1 parent a8e8017 commit 79d812e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Python/longest-word-in-dictionary.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Time: O(n), n is the total sum of the lengths of words
22
# Space: O(t), t is the number of nodes in trie
33

4-
import collections
4+
from collections import defaultdict
5+
from operator import getitem
56

67

78
class Solution(object):
@@ -10,10 +11,10 @@ def longestWord(self, words):
1011
:type words: List[str]
1112
:rtype: str
1213
"""
13-
_trie = lambda: collections.defaultdict(_trie)
14+
_trie = lambda: defaultdict(_trie)
1415
trie = _trie()
1516
for i, word in enumerate(words):
16-
reduce(dict.__getitem__, word, trie)["_end"] = i
17+
reduce(getitem, word, trie)["_end"] = i
1718

1819
# DFS
1920
stack = trie.values()

0 commit comments

Comments
 (0)