Skip to content

Commit 93cd952

Browse files
committed
feat: add solutions to leetcode problem: No.1143. Longest Common Subsequence
1 parent a4ecfd8 commit 93cd952

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
- [打家劫舍 II](/solution/0200-0299/0213.House%20Robber%20II/README.md)
159159
- [最长上升子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
160160
- [俄罗斯套娃信封问题](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
161+
- [最长公共子序列](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
161162

162163
### 回溯算法
163164

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
152152
- [House Robber II](/solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
153153
- [Longest Increasing Subsequence](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
154154
- [Russian Doll Envelopes](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
155+
- [Longest Common Subsequence](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)
155156

156157
### Backtracking
157158

solution/1100-1199/1143.Longest Common Subsequence/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,49 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
动态规划法。
53+
54+
定义 `dp[i][j]` 表示 `text1[0:i-1]``text2[0:j-1]` 的最长公共子序列(闭区间)。
55+
56+
递推公式如下:
57+
58+
![](./images/gif.gif)
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**
5563

5664
<!-- 这里可写当前语言的特殊实现逻辑 -->
5765

5866
```python
59-
67+
class Solution:
68+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
69+
m, n = len(text1), len(text2)
70+
dp = [[0] * (n + 1) for _ in range(m + 1)]
71+
for i in range(1, m + 1):
72+
for j in range(1, n + 1):
73+
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
74+
return dp[m][n]
6075
```
6176

6277
### **Java**
6378

6479
<!-- 这里可写当前语言的特殊实现逻辑 -->
6580

6681
```java
67-
82+
class Solution {
83+
public int longestCommonSubsequence(String text1, String text2) {
84+
int m = text1.length(), n = text2.length();
85+
int[][] dp = new int[m + 1][n + 1];
86+
for (int i = 1; i <= m; ++i) {
87+
for (int j = 1; j <= n; ++j) {
88+
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
89+
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
90+
}
91+
}
92+
return dp[m][n];
93+
}
94+
}
6895
```
6996

7097
### **...**

solution/1100-1199/1143.Longest Common Subsequence/README_EN.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
58+
m, n = len(text1), len(text2)
59+
dp = [[0] * (n + 1) for _ in range(m + 1)]
60+
for i in range(1, m + 1):
61+
for j in range(1, n + 1):
62+
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
63+
return dp[m][n]
5764
```
5865

5966
### **Java**
6067

6168
```java
62-
69+
class Solution {
70+
public int longestCommonSubsequence(String text1, String text2) {
71+
int m = text1.length(), n = text2.length();
72+
int[][] dp = new int[m + 1][n + 1];
73+
for (int i = 1; i <= m; ++i) {
74+
for (int j = 1; j <= n; ++j) {
75+
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
76+
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
77+
}
78+
}
79+
return dp[m][n];
80+
}
81+
}
6382
```
6483

6584
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int longestCommonSubsequence(String text1, String text2) {
3+
int m = text1.length(), n = text2.length();
4+
int[][] dp = new int[m + 1][n + 1];
5+
for (int i = 1; i <= m; ++i) {
6+
for (int j = 1; j <= n; ++j) {
7+
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
8+
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
9+
}
10+
}
11+
return dp[m][n];
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
m, n = len(text1), len(text2)
4+
dp = [[0] * (n + 1) for _ in range(m + 1)]
5+
for i in range(1, m + 1):
6+
for j in range(1, n + 1):
7+
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
8+
return dp[m][n]
Loading

0 commit comments

Comments
 (0)