Skip to content

Commit ae0d605

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent afcd7a1 commit ae0d605

File tree

1 file changed

+283
-0
lines changed
  • Solution/1143. Longest Common Subsequence

1 file changed

+283
-0
lines changed
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md
5+
tags:
6+
- String
7+
- Dynamic Programming
8+
---
9+
10+
<!-- problem:start -->
11+
12+
# [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence)
13+
14+
[中文文档](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
15+
16+
## Description
17+
18+
<!-- description:start -->
19+
20+
<p>Given two strings <code>text1</code> and <code>text2</code>, return <em>the length of their longest <strong>common subsequence</strong>. </em>If there is no <strong>common subsequence</strong>, return <code>0</code>.</p>
21+
22+
<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>
23+
24+
<ul>
25+
<li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li>
26+
</ul>
27+
28+
<p>A <strong>common subsequence</strong> of two strings is a subsequence that is common to both strings.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong class="example">Example 1:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> text1 = &quot;abcde&quot;, text2 = &quot;ace&quot;
35+
<strong>Output:</strong> 3
36+
<strong>Explanation:</strong> The longest common subsequence is &quot;ace&quot; and its length is 3.
37+
</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;abc&quot;
43+
<strong>Output:</strong> 3
44+
<strong>Explanation:</strong> The longest common subsequence is &quot;abc&quot; and its length is 3.
45+
</pre>
46+
47+
<p><strong class="example">Example 3:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;def&quot;
51+
<strong>Output:</strong> 0
52+
<strong>Explanation:</strong> There is no such common subsequence, so the result is 0.
53+
</pre>
54+
55+
<p>&nbsp;</p>
56+
<p><strong>Constraints:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= text1.length, text2.length &lt;= 1000</code></li>
60+
<li><code>text1</code> and <code>text2</code> consist of only lowercase English characters.</li>
61+
</ul>
62+
63+
<!-- description:end -->
64+
65+
## Solutions
66+
67+
<!-- solution:start -->
68+
69+
### Solution 1: Dynamic Programming
70+
71+
We define $f[i][j]$ as the length of the longest common subsequence of the first $i$ characters of $text1$ and the first $j$ characters of $text2$. Therefore, the answer is $f[m][n]$, where $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.
72+
73+
If the $i$th character of $text1$ and the $j$th character of $text2$ are the same, then $f[i][j] = f[i - 1][j - 1] + 1$; if the $i$th character of $text1$ and the $j$th character of $text2$ are different, then $f[i][j] = max(f[i - 1][j], f[i][j - 1])$. The state transition equation is:
74+
75+
$$
76+
f[i][j] =
77+
\begin{cases}
78+
f[i - 1][j - 1] + 1, & \textit{if } text1[i - 1] = text2[j - 1] \\
79+
\max(f[i - 1][j], f[i][j - 1]), & \textit{if } text1[i - 1] \neq text2[j - 1]
80+
\end{cases}
81+
$$
82+
83+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.
84+
85+
<!-- tabs:start -->
86+
87+
#### Python3
88+
89+
```python
90+
class Solution:
91+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
92+
m, n = len(text1), len(text2)
93+
f = [[0] * (n + 1) for _ in range(m + 1)]
94+
for i in range(1, m + 1):
95+
for j in range(1, n + 1):
96+
if text1[i - 1] == text2[j - 1]:
97+
f[i][j] = f[i - 1][j - 1] + 1
98+
else:
99+
f[i][j] = max(f[i - 1][j], f[i][j - 1])
100+
return f[m][n]
101+
```
102+
103+
#### Java
104+
105+
```java
106+
class Solution {
107+
public int longestCommonSubsequence(String text1, String text2) {
108+
int m = text1.length(), n = text2.length();
109+
int[][] f = new int[m + 1][n + 1];
110+
for (int i = 1; i <= m; ++i) {
111+
for (int j = 1; j <= n; ++j) {
112+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
113+
f[i][j] = f[i - 1][j - 1] + 1;
114+
} else {
115+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
116+
}
117+
}
118+
}
119+
return f[m][n];
120+
}
121+
}
122+
```
123+
124+
#### C++
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int longestCommonSubsequence(string text1, string text2) {
130+
int m = text1.size(), n = text2.size();
131+
int f[m + 1][n + 1];
132+
memset(f, 0, sizeof f);
133+
for (int i = 1; i <= m; ++i) {
134+
for (int j = 1; j <= n; ++j) {
135+
if (text1[i - 1] == text2[j - 1]) {
136+
f[i][j] = f[i - 1][j - 1] + 1;
137+
} else {
138+
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
139+
}
140+
}
141+
}
142+
return f[m][n];
143+
}
144+
};
145+
```
146+
147+
#### Go
148+
149+
```go
150+
func longestCommonSubsequence(text1 string, text2 string) int {
151+
m, n := len(text1), len(text2)
152+
f := make([][]int, m+1)
153+
for i := range f {
154+
f[i] = make([]int, n+1)
155+
}
156+
for i := 1; i <= m; i++ {
157+
for j := 1; j <= n; j++ {
158+
if text1[i-1] == text2[j-1] {
159+
f[i][j] = f[i-1][j-1] + 1
160+
} else {
161+
f[i][j] = max(f[i-1][j], f[i][j-1])
162+
}
163+
}
164+
}
165+
return f[m][n]
166+
}
167+
```
168+
169+
#### TypeScript
170+
171+
```ts
172+
function longestCommonSubsequence(text1: string, text2: string): number {
173+
const m = text1.length;
174+
const n = text2.length;
175+
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
176+
for (let i = 1; i <= m; i++) {
177+
for (let j = 1; j <= n; j++) {
178+
if (text1[i - 1] === text2[j - 1]) {
179+
f[i][j] = f[i - 1][j - 1] + 1;
180+
} else {
181+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
182+
}
183+
}
184+
}
185+
return f[m][n];
186+
}
187+
```
188+
189+
#### Rust
190+
191+
```rust
192+
impl Solution {
193+
pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
194+
let (m, n) = (text1.len(), text2.len());
195+
let (text1, text2) = (text1.as_bytes(), text2.as_bytes());
196+
let mut f = vec![vec![0; n + 1]; m + 1];
197+
for i in 1..=m {
198+
for j in 1..=n {
199+
f[i][j] = if text1[i - 1] == text2[j - 1] {
200+
f[i - 1][j - 1] + 1
201+
} else {
202+
f[i - 1][j].max(f[i][j - 1])
203+
};
204+
}
205+
}
206+
f[m][n]
207+
}
208+
}
209+
```
210+
211+
#### JavaScript
212+
213+
```js
214+
/**
215+
* @param {string} text1
216+
* @param {string} text2
217+
* @return {number}
218+
*/
219+
var longestCommonSubsequence = function (text1, text2) {
220+
const m = text1.length;
221+
const n = text2.length;
222+
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
223+
for (let i = 1; i <= m; ++i) {
224+
for (let j = 1; j <= n; ++j) {
225+
if (text1[i - 1] == text2[j - 1]) {
226+
f[i][j] = f[i - 1][j - 1] + 1;
227+
} else {
228+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
229+
}
230+
}
231+
}
232+
return f[m][n];
233+
};
234+
```
235+
236+
#### C#
237+
238+
```cs
239+
public class Solution {
240+
public int LongestCommonSubsequence(string text1, string text2) {
241+
int m = text1.Length, n = text2.Length;
242+
int[,] f = new int[m + 1, n + 1];
243+
for (int i = 1; i <= m; ++i) {
244+
for (int j = 1; j <= n; ++j) {
245+
if (text1[i - 1] == text2[j - 1]) {
246+
f[i, j] = f[i - 1, j - 1] + 1;
247+
} else {
248+
f[i, j] = Math.Max(f[i - 1, j], f[i, j - 1]);
249+
}
250+
}
251+
}
252+
return f[m, n];
253+
}
254+
}
255+
```
256+
257+
#### Kotlin
258+
259+
```kotlin
260+
class Solution {
261+
fun longestCommonSubsequence(text1: String, text2: String): Int {
262+
val m = text1.length
263+
val n = text2.length
264+
val f = Array(m + 1) { IntArray(n + 1) }
265+
for (i in 1..m) {
266+
for (j in 1..n) {
267+
if (text1[i - 1] == text2[j - 1]) {
268+
f[i][j] = f[i - 1][j - 1] + 1
269+
} else {
270+
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1])
271+
}
272+
}
273+
}
274+
return f[m][n]
275+
}
276+
}
277+
```
278+
279+
<!-- tabs:end -->
280+
281+
<!-- solution:end -->
282+
283+
<!-- problem:end -->

0 commit comments

Comments
 (0)