LCS and Subsequence Problems
LCS and Subsequence Problems
2. Print LCS
4. Print SCS
Let me know if you want explanations or solutions for any of these problems!
This is a concept note for Longest Common Subsequence (LCS) problem, illustrated with an
example and approach breakdown. Let's break this down in simple steps and then
implement JavaScript code for LCS.
1/9
📌 Understanding the Diagram:
Problem:
Find Longest Common Subsequence (LCS) of two strings x and y .
Example:
ini
x = "abcdgh"
y = "abedfhr"
1. Step-by-step Explanation:
A) Inputs:
x : "abcdgh"
y : "abedfhr"
B) Output:
LCS: "abdh"
Length: 4
2. Approach Recap:
Recursive Relation:
2/9
3. Flow to Solve:
mathematica
1. Problem Statement
2. Recursive Discussion (Find recursive relation)
3. Base Cases
4. DP Table (Bottom-Up)
5. Print LCS
4. Visualization in Matrix:
Fill table based on matches:
css
a b e d f h r
a 1 1 1 1 1 1 1
b 1 2 2 2 2 2 2
c 1 2 2 2 2 2 2
d 1 2 2 3 3 3 3
g 1 2 2 3 3 3 3
h 1 2 2 3 3 4 4
javascript
function lcsLength(x, y) {
const m = x.length;
const n = y.length;
3/9
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
return dp[m][n];
}
const x = "abcdgh";
const y = "abedfhr";
console.log("Length of LCS:", lcsLength(x, y)); // Output: 4
javascript
function printLCS(x, y) {
const m = x.length;
const n = y.length;
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
// Fill DP table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (x[i - 1] === y[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
4/9
// Backtracking to find LCS
let i = m, j = n;
let lcsStr = [];
return lcsStr.reverse().join('');
}
const x1 = "abcdgh";
const y1 = "abedfhr";
console.log("LCS:", printLCS(x1, y1)); // Output: "abdh"
✅ Summary of Flow:
Step Description
5/9
💡 Important Notes:
Recursion helps understand problem flow (Top-down).
Can also modify for variations like printing all LCS, counting number of LCS, shortest
common supersequence, etc.
If you want, I can also explain recursive to DP conversion step-by-step, or how to handle
LCS variants. Want that? 🚀
Great! Let's now explain the recursive approach of LCS step by step and add it properly to
your document.
📌 Recursive Formula:
Case 1: Characters Match
If the last characters of both strings match, we include this character in LCS and recursively
solve for the remaining strings.
6/9
LCS(x, y, m, n) = 1 + LCS(x, y, m-1, n-1)
If the last characters do not match, we skip one character either from x or y and take the
maximum of both options:
lua
📌 Base Case:
If either m == 0 or n == 0 , meaning any one string is empty, LCS is 0:
kotlin
if (m == 0 || n == 0) return 0;
function lcsRecursive(x, y, m, n) {
// Base Case: If either string is empty
if (m === 0 || n === 0) return 0;
7/9
);
}
}
// Example
const x = "abcdgh";
const y = "abedfhr";
console.log("Length of LCS (Recursive):", lcsRecursive(x, y, x.length, y.length));
// Output: 4
🔑 Key Points:
Concept Description
Recursive Sub-problems Break problem into smaller subproblems by removing last char
👉 Next Step (Optional): Explain how to optimize this recursive solution using Memoization
(Top-Down DP) to avoid recomputation.
8/9
If you want, I can add Memoization version too in this document. Want me to proceed? 🚀
The memoization version has been added to the document along with a complete
explanation and code example! 🚀
If you'd like, we can now proceed to Tabulation (Bottom-Up DP) approach or explore LCS
variations like printing all LCS sequences. Let me know! 💡✨
Tabulation (Bottom-Up DP) has been successfully added to your document with full
JavaScript implementation and explanation! ✅
If you now want, we can move on to printing the actual LCS string from the DP table or
explore other variations like Shortest Common Supersequence (SCS). Let me know what
you prefer! 🚀
9/9