Skip to content

Commit 1288183

Browse files
committed
longest-common-subsequence solved
1 parent 4225cb8 commit 1288183

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} text1
3+
* @param {string} text2
4+
* @return {number}
5+
*/
6+
var longestCommonSubsequence = function(text1, text2) {
7+
const m = text1.length;
8+
const n = text2.length;
9+
10+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
11+
12+
for (let i = m - 1; i >= 0; i--) {
13+
for (let j = n - 1; j >= 0; j--) {
14+
if (text1[i] === text2[j]) {
15+
dp[i][j] = 1 + dp[i + 1][j + 1];
16+
} else {
17+
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]);
18+
}
19+
}
20+
}
21+
22+
return dp[0][0];
23+
};

0 commit comments

Comments
 (0)