Skip to content

Commit fbf4ce3

Browse files
committed
Create 820.单词的压缩编码.js
1 parent 17e381e commit fbf4ce3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

820.单词的压缩编码.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
var minimumLengthEncoding = function(words) {
6+
words.sort((a, b) => b.length - a.length);
7+
const map = new Map();
8+
for (const word of words) {
9+
const list = map.get(word[word.length - 1]) || [];
10+
let i = 0;
11+
while (i < list.length) {
12+
if (list[i].lastIndexOf(word) === list[i].length - word.length) {
13+
break;
14+
}
15+
i++;
16+
}
17+
if (i === list.length) {
18+
list.push(word);
19+
map.set(word[word.length - 1], list);
20+
}
21+
}
22+
let result = 0;
23+
for (const list of map.values()) {
24+
for (const word of list) {
25+
result += word.length + 1;
26+
}
27+
}
28+
return result;
29+
};

0 commit comments

Comments
 (0)