---
# System prepended metadata

title: LC 1143. Longest Common Subsequence
tags: [medium, leedcode, DP, LCS, python, c++]

---

# LC 1143. Longest Common Subsequence

### [Problem link](https://leetcode.com/problems/longest-common-subsequence/)

###### tags: `leedcode` `python` `c++` `medium` `DP` `LCS`

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

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.

- For example, <code>"ace"</code> is a subsequence of <code>"abcde"</code>.

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:** 

- <code>1 <= text1.length, text2.length <= 1000</code>
- <code>text1</code> and <code>text2</code> consist of only lowercase English characters.

## Solution 1 - DP
#### Python
```python=
class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        m, n = len(text1), len(text2)
        res = 0
        dp = [[0] * (n + 1) for _ in range(m + 1)]
        # dp[i][j] = 以nums1[i - 1]為結尾與以nums2[j - 1]為結尾的Longest Common Subsequence

        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if text1[i - 1] == text2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1] + 1
                else:
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

        return dp[-1][-1]
```
#### C++
```cpp=
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        vector<vector<int>> dp(text1.size() + 1, vector<int>(text2.size() + 1, 0));
        for (int i = 0; i < text1.size(); i++) {
            for (int j = 0; j < text2.size(); j++) {
                if (text1[i] == text2[j]) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                } else {
                    dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
                }
            }
        }
        return dp.back().back();
    }
};
```

>### Complexity
>m = len(text1)
>n = len(text2)
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(mn)           | O(mn)            |


## Note
[代碼隨想錄](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/1143.%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97.md)
