Skip to content

Commit

Permalink
feat: longest common subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
anniemon committed Feb 1, 2025
1 parent 87fb4c0 commit bf8f275
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions longest-common-subsequence/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 시간 복잡도:
* text1과 text2의 길이로 구성된 2차원 매트릭스를 순회하므로,
* text1의 길이를 n, text2의 길이를 m이라고 하면 O(m*n)
* 공간 복잡도:
* text1과 text2의 길이로 구성된 2차원 매트릭스를 생성하므로, O(m*n)
*/
/**
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function(text1, text2) {
const dp = new Array(text1.length+1).fill(0).map(e => new Array(text2.length+1).fill(0));
for(let i = text1.length -1; i >= 0; i--) {
for(let j = text2.length-1; j >=0; j--) {
if(text1[i] === text2[j]) {
dp[i][j] = dp[i+1][j+1] + 1
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j+1])
}
}
}
return dp[0][0]
};

0 comments on commit bf8f275

Please sign in to comment.