--- 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 --- <!-- problem:start --> # [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) [中文文档](/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md) ## Description <!-- description:start --> <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> <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> <ul> <li>For example, <code>"ace"</code> is a subsequence of <code>"abcde"</code>.</li> </ul> <p>A <strong>common subsequence</strong> of two strings is a subsequence that is common to both strings.</p> <p> </p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> text1 = "abcde", text2 = "ace" <strong>Output:</strong> 3 <strong>Explanation:</strong> The longest common subsequence is "ace" and its length is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> text1 = "abc", text2 = "abc" <strong>Output:</strong> 3 <strong>Explanation:</strong> The longest common subsequence is "abc" and its length is 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text1 = "abc", text2 = "def" <strong>Output:</strong> 0 <strong>Explanation:</strong> There is no such common subsequence, so the result is 0. </pre> <p> </p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= text1.length, text2.length <= 1000</code></li> <li><code>text1</code> and <code>text2</code> consist of only lowercase English characters.</li> </ul> <!-- description:end --> ## Solutions <!-- solution:start --> ### 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. <!-- tabs:start --> #### 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] } } ``` <!-- tabs:end --> <!-- solution:end --> <!-- problem:end -->