Skip to content

Commit 78a7ea0

Browse files
committed
Added JS 1143 Longest Common Subsequence
1 parent d9d562f commit 78a7ea0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var longestCommonSubsequence = function (text1, text2) {
2+
let m = text1.length;
3+
let n = text2.length;
4+
5+
let table = new Array(m + 1).fill().map(() => new Array(n + 1).fill(0));
6+
7+
for (let i = 1; i <= m; i++) {
8+
for (let j = 1; j <= n; j++) {
9+
if (text1.charAt(i - 1) !== text2.charAt(j - 1)) {
10+
11+
table[i][j] = Math.max(table[i - 1][j], table[i][j - 1]);
12+
} else {
13+
table[i][j] = table[i - 1][j - 1] + 1;
14+
}
15+
}
16+
}
17+
return table[m][n];
18+
};

0 commit comments

Comments
 (0)