Minimum Length Encoding of Words

Given a list of words, return the length of the shortest reference string that encodes all words as suffixes separated by '#'. Pattern focus: Trie Node Structure. Reverse-word trie construction is useful for removing suffix-covered words before counting the final encoding length.

Input Format

words = array of lowercase words

Output Format

minimum encoding length

Constraints

  • 1 <= words.length <= 2 * 10^4
  • 1 <= words[i].length <= 7
  • Words contain only lowercase English letters.

Examples

Example 1:

Input:

words = ["time","me","bell"]

Output:

10

Explanation:

Only the words that are not suffixes of another word contribute to the final encoding.

Example 2:

Input:

words = ["t"]

Output:

2

Explanation:

A single word still needs the trailing separator character.

Example 3:

Input:

words = ["time","time","me"]

Output:

5

Explanation:

Duplicate words are ignored, and suffix-covered words are removed from the count.

Loading...
Minimum Length Encoding of Words - Trie