We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9ead424 commit d091018Copy full SHA for d091018
1143-longest-common-subsequence.js
@@ -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