Skip to content

Commit 4f04c9c

Browse files
week12 mission longest-common-subsequence
1 parent e92686e commit 4f04c9c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- 문제: https://leetcode.com/problems/longest-common-subsequence/
2+
- 풀이: https://algorithm.jonghoonpark.com/2024/07/17/leetcode-1143
3+
4+
```java
5+
class Solution {
6+
public int longestCommonSubsequence(String text1, String text2) {
7+
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
8+
9+
for (int i = 1; i < dp.length; i++) {
10+
for (int j = 1; j < dp[0].length; j++) {
11+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
12+
dp[i][j] = dp[i - 1][j - 1] + 1;
13+
} else {
14+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
15+
}
16+
}
17+
}
18+
19+
return dp[text1.length()][text2.length()];
20+
}
21+
}
22+
```
23+
24+
### TC, SC
25+
26+
시간 복잡도는 `O(m * n)` 공간 복잡도는 `O(m * n)` 이다.

0 commit comments

Comments
 (0)