Skip to content

Commit d091018

Browse files
authored
Create 1143-longest-common-subsequence.js
1 parent 9ead424 commit d091018

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

1143-longest-common-subsequence.js

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

0 commit comments

Comments
 (0)