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 ffa0e87 commit 098111eCopy full SHA for 098111e
longest-common-subsequence/moonjonghoo.js
@@ -0,0 +1,22 @@
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
+ n = text2.length;
9
+ const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
10
+
11
+ for (let i = 1; i <= m; i++) {
12
+ for (let j = 1; j <= n; j++) {
13
+ if (text1[i - 1] === text2[j - 1]) {
14
+ dp[i][j] = dp[i - 1][j - 1] + 1;
15
+ } else {
16
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
17
+ }
18
19
20
21
+ return dp[m][n];
22
+};
0 commit comments