Skip to content

Commit cab42de

Browse files
authored
Merge pull request #16 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents 67744a4 + eaeec76 commit cab42de

File tree

2 files changed

+293
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)