Longest Common Subsequence (LCS)
Finds the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguously. This is fundamental in diff utilities, DNA sequence analysis, and version control systems like Git.
Visualization
Interactive visualization for Longest Common Subsequence (LCS)
Longest Common Subsequence (LCS)
• Time: O(m × n) where m, n are string lengths
• Space: O(m × n) for DP table
• Used in diff tools, DNA sequence alignment, version control
Interactive visualization with step-by-step execution
Implementation
1function lcs(str1: string, str2: string): number {
2 const m = str1.length;
3 const n = str2.length;
4 const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
5
6 for (let i = 1; i <= m; i++) {
7 for (let j = 1; j <= n; j++) {
8 if (str1[i - 1] === str2[j - 1]) {
9 dp[i][j] = 1 + dp[i - 1][j - 1];
10 } else {
11 dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
12 }
13 }
14 }
15
16 return dp[m][n];
17}
18
19// Get actual LCS string
20function getLCS(str1: string, str2: string): string {
21 const m = str1.length;
22 const n = str2.length;
23 const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
24
25 for (let i = 1; i <= m; i++) {
26 for (let j = 1; j <= n; j++) {
27 if (str1[i - 1] === str2[j - 1]) {
28 dp[i][j] = 1 + dp[i - 1][j - 1];
29 } else {
30 dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
31 }
32 }
33 }
34
35 // Backtrack to find actual LCS
36 let result = '';
37 let i = m, j = n;
38 while (i > 0 && j > 0) {
39 if (str1[i - 1] === str2[j - 1]) {
40 result = str1[i - 1] + result;
41 i--;
42 j--;
43 } else if (dp[i - 1][j] > dp[i][j - 1]) {
44 i--;
45 } else {
46 j--;
47 }
48 }
49
50 return result;
51}Deep Dive
Theoretical Foundation
LCS uses a 2D DP table where dp[i][j] represents the length of the LCS of the first i characters of string1 and first j characters of string2. The solution builds up from smaller subproblems, avoiding exponential brute force complexity.
Complexity
Time
O(mn)
O(mn)
O(mn)
Space
O(mn)
Applications
Industry Use
DNA sequence comparison in bioinformatics
File diff utilities (diff, git diff)
Plagiarism detection
Data compression algorithms
Speech recognition systems
Longest common substring in databases
Use Cases
Related Algorithms
0/1 Knapsack Problem
A classic optimization problem where you must select items with given weights and values to maximize total value without exceeding the knapsack's capacity. Each item can be taken only once (0 or 1). This is a fundamental problem in combinatorial optimization, resource allocation, and decision-making scenarios.
Edit Distance (Levenshtein Distance)
Edit Distance, also known as Levenshtein Distance, computes the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another. Named after Soviet mathematician Vladimir Levenshtein who introduced it in 1965, this fundamental algorithm has applications in spell checking, DNA sequence analysis, natural language processing, and plagiarism detection.
Longest Increasing Subsequence (LIS)
Longest Increasing Subsequence (LIS) finds the length of the longest subsequence in an array where all elements are in strictly increasing order. Unlike a subarray, a subsequence doesn't need to be contiguous - elements can be selected from anywhere as long as their order is preserved. This classic DP problem has two solutions: O(n²) dynamic programming and O(n log n) binary search with patience sorting. Applications include stock price analysis, scheduling, Box Stacking Problem, and Building Bridges Problem.
Coin Change Problem
The Coin Change Problem finds the minimum number of coins needed to make a given amount using unlimited supplies of given coin denominations. It's a classic example of both dynamic programming and greedy algorithms, with two main variants: finding the minimum number of coins (optimization) and counting the number of ways to make change (counting). This problem has direct applications in currency systems, vending machines, and resource optimization.