Skip to content
LC-0820 Medium LeetCode

820. Short Encoding of Words

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 61% Topics: Array, Hash Table, String, Trie
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n), n is the total sum of the lengths of words
# Space: O(t), t is the number of nodes in trie

import collections
import functools


class Solution(object):
    def minimumLengthEncoding(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        words = list(set(words))
        _trie = lambda: collections.defaultdict(_trie)
        trie = _trie()

        nodes = [functools.reduce(dict.__getitem__, word[::-1], trie)
                 for word in words]

        return sum(len(word) + 1
                   for i, word in enumerate(words)
                   if len(nodes[i]) == 0)

Solution from kamyu104/LeetCode-Solutions · MIT