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