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 aaf516a commit fcf9aa7Copy full SHA for fcf9aa7
longest-common-subsequence/TonyKim9401.java
@@ -0,0 +1,23 @@
1
+// TC: O(n * m)
2
+// the length of text1 by the length of text2
3
+// SC: O(n)
4
+// both size of text1 and text2 can be the size of dp
5
+class Solution {
6
+ public int longestCommonSubsequence(String text1, String text2) {
7
+ int[] dp = new int[text2.length()];
8
+ int output = 0;
9
+
10
+ for (char c : text1.toCharArray()) {
11
+ int curLength = 0;
12
13
+ for (int i = 0; i < dp.length; i++) {
14
+ if (curLength < dp[i]) curLength = dp[i];
15
+ else if (c == text2.charAt(i)) {
16
+ dp[i] = curLength + 1;
17
+ output = Math.max(output, dp[i]);
18
+ }
19
20
21
+ return output;
22
23
+}
0 commit comments