forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0820.js
35 lines (31 loc) · 750 Bytes
/
0820.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var Node = function() {
this.next = new Map();
}
var Trie = function() {
this.root = new Node();
this.nodes = new Map();
};
Trie.prototype.insert = function(word, id) {
let cur = this.root;
for (let i = word.length - 1; ~i; i--) {
let c = word[i];
if (!cur.next.get(c)) {
cur.next.set(c, new Node());
}
cur = cur.next.get(c);
}
this.nodes.set(cur, id);
};
var minimumLengthEncoding = function(words) {
let root = new Trie();
for (let i = 0; i < words.length; i++) {
root.insert(words[i], i);
}
let res = 0;
for (let [k, v] of root.nodes) {
if (k.next.size == 0) {
res += words[v].length + 1;
}
}
return res;
};