Skip to content

Commit ed629cd

Browse files
author
eunhwa99
committed
longest common subsequence
1 parent 5cd9818 commit ed629cd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int longestCommonSubsequence(String text1, String text2) {
3+
int n = text1.length();
4+
int m = text2.length();
5+
6+
int[][] dp = new int[n + 1][m + 1];
7+
8+
for (int i = 1; i <= n; i++) {
9+
for (int j = 1; j <= m; j++) {
10+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
11+
dp[i][j] = dp[i - 1][j - 1] + 1;
12+
} else {
13+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
14+
}
15+
}
16+
}
17+
18+
return dp[n][m];
19+
}
20+
}
21+
// Time Complexity: O(n * m)
22+
// Space Complexity: O(n * m)

0 commit comments

Comments
 (0)