-
Notifications
You must be signed in to change notification settings - Fork 0
/
recoverSecret.js
76 lines (72 loc) · 1.48 KB
/
recoverSecret.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var recoverSecret = function(triplets) {
formMap(triplets);
var head = findHead();
return findLongestPath(head);
};
function findLongestPath(node){
var maxNextStep = 0,maxKey;
for(var i in node.next){
findLongestPath(node.next[i]);
if(node.next[i].step > maxNextStep){
maxNextStep = node.next[i].step;
maxKey = i;
}
}
if(maxNextStep === 0 && !maxKey){
node.step = 1;
node.nextStr = node.letter;
}
else{
node.step = maxNextStep+1;
node.nextStr = node.letter+node.next[maxKey].nextStr;
}
return node.nextStr;
}
function Node(letter){
this.next = {};
this.letter = letter;
}
Node.prototype.addNext = function(node){
if(!this.next[node.letter]){
this.next[node.letter] = node;
node.hasParent = true;
}
return this;
};
var nodeMap = {};
function formMap(triplets){
nodeMap = {};
var j= 0,letter,node,pre,head;
for(var i = 0 ; i<triplets.length;i++){
for(j=0;j<triplets[i].length;j++){
letter = triplets[i][j];
if(!nodeMap[letter]){
node = new Node(letter);
nodeMap[letter] = node;
}
else{
node = nodeMap[letter];
}
if(j!==0) {
pre.addNext(node);
}
pre = node;
}
}
}
function findHead(){
for(var key in nodeMap){
if(!nodeMap[key].hasParent){
return nodeMap[key];
}
}
}
console.log(recoverSecret([
['t','u','p'],
['w','h','i'],
['t','s','u'],
['a','t','s'],
['h','a','p'],
['t','i','s'],
['w','h','s']
]));