Longest Common Subsequence - Problem
The Longest Common Subsequence Problem
You're given two strings
๐ What's a subsequence?
A subsequence is formed by deleting some (possibly zero) characters from the original string while keeping the remaining characters in their original order. For example,
๐ฏ What's a common subsequence?
It's a subsequence that appears in both strings. The longest one is what we're after!
Examples:
โข
โข
โข
Return the length of the longest common subsequence, or 0 if none exists.
You're given two strings
text1 and text2. Your mission is to find the longest common subsequence between them and return its length.๐ What's a subsequence?
A subsequence is formed by deleting some (possibly zero) characters from the original string while keeping the remaining characters in their original order. For example,
"ace" is a subsequence of "abcde".๐ฏ What's a common subsequence?
It's a subsequence that appears in both strings. The longest one is what we're after!
Examples:
โข
"abcde" and "ace" โ LCS = "ace" (length 3)โข
"abc" and "abc" โ LCS = "abc" (length 3)โข
"abc" and "def" โ No common subsequence (length 0)Return the length of the longest common subsequence, or 0 if none exists.
Input & Output
example_1.py โ Basic Common Subsequence
$
Input:
text1 = "abcde", text2 = "ace"
โบ
Output:
3
๐ก Note:
The longest common subsequence is "ace" with length 3. We can form "ace" from both strings by selecting appropriate characters while maintaining order.
example_2.py โ Different Characters
$
Input:
text1 = "abc", text2 = "abc"
โบ
Output:
3
๐ก Note:
When both strings are identical, the entire string is the longest common subsequence. Length is 3.
example_3.py โ No Common Characters
$
Input:
text1 = "abc", text2 = "def"
โบ
Output:
0
๐ก Note:
There are no common characters between the strings, so the longest common subsequence has length 0.
Visualization
Tap to expand
Understanding the Visualization
1
Set Up Laboratory Grid
Create a grid where each cell represents comparing characters from both DNA sequences
2
Compare Gene by Gene
When genes match, extend the best previous alignment. Record the improved match length.
3
Handle Mismatches
When genes don't match, take the better result from either skipping current gene in sequence 1 or sequence 2
4
Find Optimal Alignment
The bottom-right cell contains the length of the longest common genetic sequence
Key Takeaway
๐ฏ Key Insight: Dynamic Programming transforms an exponential problem into polynomial time by storing intermediate results. Each cell builds upon previous optimal solutions, ensuring we find the globally optimal answer efficiently.
Time & Space Complexity
Time Complexity
O(m ร n)
We fill each cell of the mรn DP table exactly once
โ Linear Growth
Space Complexity
O(m ร n)
Space for the 2D DP table of size (m+1)ร(n+1)
โก Linearithmic Space
Constraints
- 1 โค text1.length, text2.length โค 1000
- text1 and text2 consist of only lowercase English characters.
- Time limit: 2 seconds
- Space limit: 256 MB
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code