Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 1143.longest-common-subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* URL of this problem
* https://leetcode.com/problems/longest-common-subsequence/
*/

/**
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function (text1, text2) {
const leng1 = text1.length;
const leng2 = text2.length;
const dp = [...new Array(leng1 + 1)].map((ele) => new Array(leng2 + 1).fill(0));

for (let i = 1; i < leng1 + 1; i++) {
for (let j = 1; j < leng2 + 1; j++) {
text1[i - 1] === text2[j - 1] ? (dp[i][j] = dp[i - 1][j - 1] + 1) : (dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]));
}
}

return dp[leng1][leng2];
};