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 7561b4e commit d5373fdCopy full SHA for d5373fd
longest-common-subsequence/HC-kang.ts
@@ -0,0 +1,23 @@
1
+/**
2
+ * https://leetcode.com/problems/longest-common-subsequence
3
+ * T.C. O(m * n)
4
+ * S.C. O(n)
5
+ */
6
+function longestCommonSubsequence(text1: string, text2: string): number {
7
+ const dp = Array.from({ length: text2.length + 1 }, () => 0);
8
+
9
+ for (let i = 1; i <= text1.length; i++) {
10
+ let prev = 0;
11
+ for (let j = 1; j <= text2.length; j++) {
12
+ const temp = dp[j];
13
+ if (text1[i - 1] === text2[j - 1]) {
14
+ dp[j] = prev + 1;
15
+ } else {
16
+ dp[j] = Math.max(dp[j], dp[j - 1]);
17
+ }
18
+ prev = temp;
19
20
21
22
+ return dp[text2.length];
23
+}
0 commit comments