Skip to content

Commit 0e20476

Browse files
authored
Merge pull request neetcode-gh#105 from anthonysim/asim/longcommonsub
Added JS 1143 Longest Common Subsequence
2 parents badc437 + 927c786 commit 0e20476

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
table[i][j] = Math.max(table[i - 1][j], table[i][j - 1]);
11+
} else {
12+
table[i][j] = table[i - 1][j - 1] + 1;
13+
}
14+
}
15+
}
16+
return table[m][n];
17+
};

0 commit comments

Comments
 (0)