-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLongest String Chain.js
57 lines (52 loc) · 1.85 KB
/
Longest String Chain.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
/**
* @param {string[]} words
* @return {number}
*/
var longestStrChain = function(words)
{
let tiers = new Array(16);
for(let i=0; i<tiers.length; i++)
tiers[i] = [];
for(let word of words)
tiers[word.length-1].push({word,len:1});
const isPredecessor = function(word1,word2) // Assumes word2.length = word1.length+1
{
let w1p = 0, misses = 0;
for(let w2p = 0; w2p < word2.length; w2p++)
{
if(word2[w2p] !== word1[w1p])
{
if(misses === 1)
return false;
misses = 1;
}
else
{
w1p++;
if(w1p === word1.length)
return true;
}
}
return true;
};
for(let i=tiers.length-1; i>0; i--)
{
for(let w2=0; w2<tiers[i].length; w2++)
{
for(let w1 = 0; w1 < tiers[i-1].length; w1++)
{
if(tiers[i-1][w1].len >= tiers[i][w2].len+1)
continue;
if(isPredecessor(tiers[i-1][w1].word, tiers[i][w2].word))
tiers[i-1][w1].len = tiers[i][w2].len+1;
}
}
}
let max = 0;
for(let i=0; i<tiers.length; i++)
{
for(let j=0; j<tiers[i].length; j++)
max = Math.max(max,tiers[i][j].len);
}
return max;
};