给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A。
例如,如果这个列表是 [“time”, “me”, “bell”],我们就可以将其表示为 S = “time#bell#” 和 indexes = [0, 2, 5]。
对于每一个索引,我们可以通过从字符串 S 中索引的位置开始读取字符串,直到 “#” 结束,来恢复我们之前的单词列表。
那么成功对给定单词列表进行编码的最小字符串长度是多少呢?
Given a list of words, we may encode it by writing a reference string S and a list of indexes A.
For example, if the list of words is [“time”, “me”, “bell”], we can write it as S = “time#bell#” and indexes = [0, 2, 5].
Then for each index, we will recover the word by reading from the reference string from that index until we reach a “#” character.
What is the length of the shortest reference string S possible that encodes the given words?
示例:
输入: words = [“time”, “me”, “bell”]
输出: 10
说明: S = “time#bell#” , indexes = [0, 2, 5] 。
思路
(1)参见:
99% Trie 吐血攻略,包教包会——by Sweetiee
无需字典树,轻轻一反转,结果就出来——by nettee
(2)对于字典树的不了解,wait.
解法1-字典树
class Solution {
public int minimumLengthEncoding(String[] words) {
int len = 0;
Trie trie = new Trie();
// 先对单词列表根据单词长度由长到短排序
Arrays.sort(words, (s1, s2) -> s2.length() - s1.length());
// 单词插入trie,返回该单词增加的编码长度
for (String word: words) {
len += trie.insert(word);
}
return len;
}
}
// 定义tire
class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
public int insert(String word) {
TrieNode cur = root;
boolean isNew = false;
// 倒着插入单词
for (int i = word.length() - 1; i >= 0; i--) {
int c = word.charAt(i) - 'a';
if (cur.children[c] == null) {
isNew = true; // 是新单词
cur.children[c] = new TrieNode();
}
cur = cur.children[c];
}
// 如果是新单词的话编码长度增加新单词的长度+1,否则不变。
return isNew? word.length() + 1: 0;
}
}
class TrieNode {
char val;
TrieNode[] children = new TrieNode[26];
public TrieNode() {}
}
作者:sweetiee
链接:https://leetcode-cn.com/problems/short-encoding-of-words/solution/99-java-trie-tu-xie-gong-lue-bao-jiao-bao-hui-by-s/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解法2-1 Trie
public int minimumLengthEncoding(String[] words) {
int N = words.length;
// 反转每个单词
String[] reversed_words = new String[N];
for (int i = 0; i < N; i++) {
String word = words[i];
String rword = new StringBuilder(word).reverse().toString();
reversed_words[i] = rword;
}
// 字典序排序
Arrays.sort(reversed_words);
int res = 0;
for (int i = 0; i < N; i++) {
if (i+1 < N && reversed_words[i+1].startsWith(reversed_words[i])) {
// 当前单词是下一个单词的前缀,丢弃
} else {
res += reversed_words[i].length() + 1; // 单词加上一个 '#' 的长度
}
}
return res;
}
作者:nettee
链接:https://leetcode-cn.com/problems/short-encoding-of-words/solution/wu-xu-zi-dian-shu-qing-qing-yi-fan-zhuan-jie-guo-j/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解法2-2
(1)优化,省去反转后的数组。
public int minimumLengthEncoding(String[] words) {
int N = words.length;
Comparator<String> cmp = (s1, s2) -> {
int N1 = s1.length();
int N2 = s2.length();
for (int i = 0; i < Math.min(N1, N2); i++) {
char c1 = s1.charAt(N1 - 1 - i);
char c2 = s2.charAt(N2 - 1 - i);
int c = Character.compare(c1, c2);
if (c != 0) {
return c;
}
}
return Integer.compare(N1, N2);
};
// 逆序字典序排序
Arrays.sort(words, cmp);
int res = 0;
for (int i = 0; i < N; i++) {
if (i+1 < N && words[i+1].endsWith(words[i])) {
// 当前单词是下一个单词的后缀,丢弃
} else {
res += words[i].length() + 1; // 单词加上一个 '#' 的长度
}
}
return res;
}
作者:nettee
链接:https://leetcode-cn.com/problems/short-encoding-of-words/solution/wu-xu-zi-dian-shu-qing-qing-yi-fan-zhuan-jie-guo-j/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。