Longest Common Subsequence - Problem
The Longest Common Subsequence Problem

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
๐Ÿงฌ DNA Sequence Alignment LaboratoryDNA Strand 1: ACGTDNA Strand 2:ACGACGT111112221233Key Insights:Gene Match: Extend previous best + 1Gene Mismatch: Take max from neighbors๐Ÿ”ฌ Lab Process:1. Compare each gene pair systematically2. Record best alignment at each step3. Build optimal solution incrementally4. Final cell = longest common sequence๐ŸŽฏ Result: DNA sequences share 3 common genes in order!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(m ร— n)

Space for the 2D DP table of size (m+1)ร—(n+1)

n
2n
โšก 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
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
125.0K Views
Very High Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen