-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcs.js
141 lines (127 loc) · 3.01 KB
/
lcs.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function lcs1(s, r, i = 0, j = 0)
{
if (i == s.length || j == r.length)
return 0
if (s[i] == r[j])
return 1 + lcs1(s, r, i + 1, j + 1)
else
return Math.max(
lcs1(s, r, i + 1, j),
lcs1(s, r, i, j + 1)
)
}
function lcs2(s, r, sl = s.length - 1, rl = r.length - 1)
{
if (sl == -1 || rl == -1)
return 0
if (s[sl] == r[rl])
return 1 + lcs2(s, r, sl - 1, rl - 1)
return Math.max(
lcs2(s, r, sl - 1, rl),
lcs2(s, r, sl, rl - 1)
)
}
function lcs3(s, r, i = 0, j = 0, val = 0)
{
if (i == s.length || j == r.length)
return val
if (s[i] == r[j])
return lcs3(s, r, i + 1, j + 1, val + 1)
else
return Math.max(
lcs3(s, r, i + 1, j, val),
lcs3(s, r, i, j + 1, val)
)
}
function lcs4(s, r, sl = s.length - 1, rl = r.length - 1, val = 0)
{
if (sl == -1 || rl == -1)
return val
if (s[sl] == r[rl])
return lcs4(s, r, sl - 1, rl - 1, val + 1)
return Math.max(
lcs4(s, r, sl - 1, rl, val),
lcs4(s, r, sl, rl - 1, val)
)
}
function lcsMemoHelper(s, r, cache, i = 0, j = 0)
{
if (i == s.length || j == r.length)
return 0
if (cache[i][j] == -1)
{
if (s[i] == r[j])
cache[i][j] = 1 + lcsMemoHelper(s, r, cache, i + 1, j + 1)
else
cache[i][j] = Math.max(
lcsMemoHelper(s, r, cache, i + 1, j),
lcsMemoHelper(s, r, cache, i, j + 1)
)
}
return cache[i][j]
}
function lcsMemo(s, r)
{
let cache = new Array(s.length)
for (let i = 0; i < s.length; i++) {
cache[i] = (new Array(r.length)).fill(-1)
}
let res = lcsMemoHelper(s, r, cache);
// printTable(cache, s.length, r.length)
return res
}
function lcsDPHelper(s, r, cache, i = s.length - 1, j = r.length - 1)
{
if (i == -1 || j == -1)
return 0
if (cache[i][j] == -1)
{
if (s[i] == r[j])
cache[i][j] = 1 + lcsDPHelper(s, r, cache, i - 1, j - 1)
else
cache[i][j] = Math.max(
lcsDPHelper(s, r, cache, i - 1, j),
lcsDPHelper(s, r, cache, i, j - 1)
)
}
return cache[i][j]
}
function lcsDynamicProgramming(s, r)
{
let cache = new Array(s.length)
for (let i = 0; i < s.length; i++) {
cache[i] = (new Array(r.length)).fill(-1)
}
let res = lcsDPHelper(s, r, cache)
// printTable(cache, s.length, r.length)
return res
}
function printTable(A, r, c)
{
let s = ''
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
s += A[i][j] + ' '
}
s += '\n'
}
console.log(s)
}
console.time("lcs1")
console.log(lcs1("AGGTAB", "GXTXAYB"))
console.timeEnd("lcs1")
console.time("lcs2")
console.log(lcs2("AGGTAB", "GXTXAYB"))
console.timeEnd("lcs2")
console.time("lcs3")
console.log(lcs3("AGGTAB", "GXTXAYB"))
console.timeEnd("lcs3")
console.time("lcs4")
console.log(lcs4("AGGTAB", "GXTXAYB"))
console.timeEnd("lcs4")
console.time("lcsMemo")
console.log(lcsMemo("sally␣sells␣sea␣shells␣by␣the␣seashore", "sarah␣sold␣salt␣sellers␣at␣the␣salt␣mines"))
console.timeEnd("lcsMemo")
console.time("lcsDynamicProgramming")
console.log(lcsDynamicProgramming("sally␣sells␣sea␣shells␣by␣the␣seashore", "sarah␣sold␣salt␣sellers␣at␣the␣salt␣mines"))
console.timeEnd("lcsDynamicProgramming")