From 4d8fc1dac94fec2bba960dbd2190bd0419cd7d87 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:22:44 +0530 Subject: [PATCH 1/5] Create 1143. Longest Common Subsequence.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../1143. Longest Common Subsequence.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py diff --git a/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py b/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py new file mode 100644 index 0000000..e69de29 From 06bee5e72477b3077122e586407ffc482de23557 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:23:02 +0530 Subject: [PATCH 2/5] Update 1143. Longest Common Subsequence.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../1143. Longest Common Subsequence.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py b/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py index e69de29..cc8667d 100644 --- a/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py +++ b/Solution/1143. Longest Common Subsequence/1143. Longest Common Subsequence.py @@ -0,0 +1,11 @@ +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + m, n = len(text1), len(text2) + f = [[0] * (n + 1) for _ in range(m + 1)] + for i in range(1, m + 1): + for j in range(1, n + 1): + if text1[i - 1] == text2[j - 1]: + f[i][j] = f[i - 1][j - 1] + 1 + else: + f[i][j] = max(f[i - 1][j], f[i][j - 1]) + return f[m][n] \ No newline at end of file From afcd7a10585a45ab0e6119035404c85d8bbd5a06 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:23:47 +0530 Subject: [PATCH 3/5] Create 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> --- Solution/1143. Longest Common Subsequence/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/1143. Longest Common Subsequence/readme.md diff --git a/Solution/1143. Longest Common Subsequence/readme.md b/Solution/1143. Longest Common Subsequence/readme.md new file mode 100644 index 0000000..e69de29 From ae0d605154b49676b66768143b773025d7dcb188 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:23:55 +0530 Subject: [PATCH 4/5] 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> --- .../readme.md | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) diff --git a/Solution/1143. Longest Common Subsequence/readme.md b/Solution/1143. Longest Common Subsequence/readme.md index e69de29..db9e7c8 100644 --- a/Solution/1143. Longest Common Subsequence/readme.md +++ b/Solution/1143. Longest Common Subsequence/readme.md @@ -0,0 +1,283 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md +tags: + - String + - Dynamic Programming +--- + + + +# [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) + +[中文文档](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md) + +## Description + + + +

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

+ +

A subsequence 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.

+ + + +

A common subsequence of two strings is a subsequence that is common to both strings.

+ +

 

+

Example 1:

+ +
+Input: text1 = "abcde", text2 = "ace" 
+Output: 3  
+Explanation: The longest common subsequence is "ace" and its length is 3.
+
+ +

Example 2:

+ +
+Input: text1 = "abc", text2 = "abc"
+Output: 3
+Explanation: The longest common subsequence is "abc" and its length is 3.
+
+ +

Example 3:

+ +
+Input: text1 = "abc", text2 = "def"
+Output: 0
+Explanation: There is no such common subsequence, so the result is 0.
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Dynamic Programming + +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. + +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: + +$$ +f[i][j] = +\begin{cases} +f[i - 1][j - 1] + 1, & \textit{if } text1[i - 1] = text2[j - 1] \\ +\max(f[i - 1][j], f[i][j - 1]), & \textit{if } text1[i - 1] \neq text2[j - 1] +\end{cases} +$$ + +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. + + + +#### Python3 + +```python +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + m, n = len(text1), len(text2) + f = [[0] * (n + 1) for _ in range(m + 1)] + for i in range(1, m + 1): + for j in range(1, n + 1): + if text1[i - 1] == text2[j - 1]: + f[i][j] = f[i - 1][j - 1] + 1 + else: + f[i][j] = max(f[i - 1][j], f[i][j - 1]) + return f[m][n] +``` + +#### Java + +```java +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int m = text1.length(), n = text2.length(); + int[][] f = new int[m + 1][n + 1]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1.charAt(i - 1) == text2.charAt(j - 1)) { + f[i][j] = f[i - 1][j - 1] + 1; + } else { + f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]); + } + } + } + return f[m][n]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int longestCommonSubsequence(string text1, string text2) { + int m = text1.size(), n = text2.size(); + int f[m + 1][n + 1]; + memset(f, 0, sizeof f); + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) { + f[i][j] = f[i - 1][j - 1] + 1; + } else { + f[i][j] = max(f[i - 1][j], f[i][j - 1]); + } + } + } + return f[m][n]; + } +}; +``` + +#### Go + +```go +func longestCommonSubsequence(text1 string, text2 string) int { + m, n := len(text1), len(text2) + f := make([][]int, m+1) + for i := range f { + f[i] = make([]int, n+1) + } + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + if text1[i-1] == text2[j-1] { + f[i][j] = f[i-1][j-1] + 1 + } else { + f[i][j] = max(f[i-1][j], f[i][j-1]) + } + } + } + return f[m][n] +} +``` + +#### TypeScript + +```ts +function longestCommonSubsequence(text1: string, text2: string): number { + const m = text1.length; + const n = text2.length; + const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; j++) { + if (text1[i - 1] === text2[j - 1]) { + f[i][j] = f[i - 1][j - 1] + 1; + } else { + f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]); + } + } + } + return f[m][n]; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let (m, n) = (text1.len(), text2.len()); + let (text1, text2) = (text1.as_bytes(), text2.as_bytes()); + let mut f = vec![vec![0; n + 1]; m + 1]; + for i in 1..=m { + for j in 1..=n { + f[i][j] = if text1[i - 1] == text2[j - 1] { + f[i - 1][j - 1] + 1 + } else { + f[i - 1][j].max(f[i][j - 1]) + }; + } + } + f[m][n] + } +} +``` + +#### JavaScript + +```js +/** + * @param {string} text1 + * @param {string} text2 + * @return {number} + */ +var longestCommonSubsequence = function (text1, text2) { + const m = text1.length; + const n = text2.length; + const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) { + f[i][j] = f[i - 1][j - 1] + 1; + } else { + f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]); + } + } + } + return f[m][n]; +}; +``` + +#### C# + +```cs +public class Solution { + public int LongestCommonSubsequence(string text1, string text2) { + int m = text1.Length, n = text2.Length; + int[,] f = new int[m + 1, n + 1]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) { + f[i, j] = f[i - 1, j - 1] + 1; + } else { + f[i, j] = Math.Max(f[i - 1, j], f[i, j - 1]); + } + } + } + return f[m, n]; + } +} +``` + +#### Kotlin + +```kotlin +class Solution { + fun longestCommonSubsequence(text1: String, text2: String): Int { + val m = text1.length + val n = text2.length + val f = Array(m + 1) { IntArray(n + 1) } + for (i in 1..m) { + for (j in 1..n) { + if (text1[i - 1] == text2[j - 1]) { + f[i][j] = f[i - 1][j - 1] + 1 + } else { + f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + } + } + } + return f[m][n] + } +} +``` + + + + + + \ No newline at end of file From eaeec766ec2b31a68c803f4957b106a85d0cc4a0 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:24:57 +0530 Subject: [PATCH 5/5] 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> --- .../1143. Longest Common Subsequence/readme.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Solution/1143. Longest Common Subsequence/readme.md b/Solution/1143. Longest Common Subsequence/readme.md index db9e7c8..084bcff 100644 --- a/Solution/1143. Longest Common Subsequence/readme.md +++ b/Solution/1143. Longest Common Subsequence/readme.md @@ -1,17 +1,16 @@ ---- -comments: true -difficulty: Medium -edit_url: https://github.com/doocs/leetcode/edit/main/solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md -tags: - - String - - Dynamic Programming ---- # [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) -[中文文档](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md) +--- +- **comments**: true +- **difficulty**: Medium +- **tags**: + - String + - Dynamic Programming +--- + ## Description