Sponsored
Sponsored
This approach involves building a trie that can be used to efficiently check if one word is a suffix of another. By storing only the unique paths in the trie, the encoding length can be minimized by pointing out the redundancies where one word is a suffix of another.
Time Complexity: O(N * K) where N is the number of words and K is the average length of a word.
Space Complexity: O(N * K) due to the storage in the Trie.
1import java.util.*;
2
3class TrieNode {
4 Map<Character, TrieNode> children = new HashMap<>();
5}
6
7class Solution {
8 public int minimumLengthEncoding(String[] words) {
9 TrieNode root = new TrieNode();
10 Set<TrieNode> leaves = new HashSet<>();
11
12 for (String word : words) {
13 TrieNode node = root;
14 for (int i = word.length() - 1; i >= 0; i--) {
15 node = node.children.computeIfAbsent(word.charAt(i), k -> new TrieNode());
16 }
17 leaves.add(node);
18 }
19
20 int length = 0;
21 for (TrieNode leaf : leaves) {
22 if (leaf.children.isEmpty()) {
23 length += word.length() + 1;
24 }
25 }
26
27 return length;
28 }
29}
The Java implementation parallels the aforementioned strategy, utilizing a HashMap to manage the TrieNode children and focusing on leaves to calculate the sum of word lengths plus '#' characters.
By reverse sorting the words and checking suffix existence, it's possible to efficiently determine the redundant entries. This alternative approach utilizes set operations to ascertain unique encodings, optimizing storage by including only necessary components.
Time Complexity: O(N * K^2), Space Complexity: O(N * K).
1class Solution:
This method sorts the words by reversed string, facilitating checks for suffixes as subsequent entries. By discarding suffixes and iterating over the unique words, it computes the total length effectively.