1
class Solution:
2
def minimumLengthEncoding(self, words):
3

4
# root of suffix trie
5
trie_root = dict()
6

7
# helper function to judge leaf node
8
isLeafNode = lambda node: len(node) == 0
9

10
# collection of tail nodes
11
tail_nodes = []
12

13
# set of unique words
14
unique_words = set(words)
15

16
# scan each word
17
for word in unique_words:
18

19
# build suffix trie from root node
20
cur = trie_root
21

22
# scan each character in reversed order
23
for char in reversed(word):
24

25
# update trie
26
cur[char] = cur.get(char, dict())
27

28
# go to next level
29
cur = cur[char]
30

31
# save tail nodes with corresponding word length, +1 is for '#' symbol
32
tail_nodes.append((cur, len(word) + 1))
33

34
# summation of the length with all tail node which is also a leaf node
35
return sum(
36
suffix_length for node, suffix_length in tail_nodes if isLeafNode(node)
37
)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0