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.
"ace" is a subsequence of "abcde".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:
1 <= text1.length, text2.length <= 1000text1 and text2 consist of only lowercase English characters.This approach uses dynamic programming to solve the problem by creating a 2D table to store the lengths of the longest common subsequences for different substrings. Each cell dp[i][j] in the table represents the longest common subsequence length of substrings text1[0..i-1] and text2[0..j-1]. The table is filled using the following rules:
text1[i-1] and text2[j-1] are equal, then dp[i][j] = dp[i-1][j-1] + 1.dp[i][j] = max(dp[i-1][j], dp[i][j-1]).The final answer is dp[text1.length][text2.length].
This C solution defines a function longestCommonSubsequence that calculates the longest common subsequence length using a 2D array dp. The program iteratively updates the table based on character matches and topological decision making (max operation). The main function demonstrates this by testing with two example strings and printing the result.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n*m), where n and m are the lengths of text1 and text2 respectively.
Space Complexity: O(n*m) for the DP table.
This optimization reduces space complexity by only storing data for the current and the previous row. The idea remains the same - calculating the LCS length incrementally using a dynamic programming strategy. However, instead of a full 2D table, only two 1D arrays are used, effectively reducing space usage from O(n*m) to O(min(n, m)). This is achieved by noting that each row of the DP table depends only on the previous row. So, we use two arrays that swap every iteration.
The optimized C solution uses two arrays previous and current to store the LCS length for the current and last iteration. After computing each row, the current becomes the previous, thereby saving space. The solution is efficient both in terms of time and space compared to the full 2D table.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n*m), where n is the length of text1 and m is the length of text2.
Space Complexity: O(min(n, m))
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n*m), where n and m are the lengths of |
| Optimized Dynamic Programming with Space Reduction | Time Complexity: O(n*m), where n is the length of |
4.9 Longest Common Subsequence (LCS) - Recursion and Dynamic Programming • Abdul Bari • 1,257,389 views views
Watch 9 more video solutions →Practice Longest Common Subsequence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor