Skip to content

Commit d72e3fd

Browse files
added 0008 Longest Common Subsequence to ( L-A )
1 parent f18425f commit d72e3fd

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 0005 Longest Common Subsequence ( L-A )
2+
3+
## Problem
4+
5+
Given two strings, find the length of their longest common subsequence (LCS). A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
6+
7+
## Example 1
8+
9+
```
10+
Input:
11+
str1 = "ABCDGH"
12+
str2 = "AEDFHR"
13+
14+
Output:
15+
The longest common subsequence is "ADH" with a length of 3.
16+
17+
Input:
18+
str1 = "AGGTAB"
19+
str2 = "GXTXAYB"
20+
21+
Output:
22+
The longest common subsequence is "GTAB" with a length of 4.
23+
```
24+
25+
## Solution Pseudocode
26+
27+
```javascript
28+
function longestCommonSubsequence(str1, str2) {
29+
const m = str1.length;
30+
const n = str2.length;
31+
32+
// Initialize a 2D array with 0
33+
const lcs = Array(m + 1)
34+
.fill()
35+
.map(() => Array(n + 1).fill(0));
36+
37+
// Fill the 2D array with LCS lengths
38+
for (let i = 1; i <= m; i++) {
39+
for (let j = 1; j <= n; j++) {
40+
if (str1[i - 1] === str2[j - 1]) {
41+
lcs[i][j] = lcs[i - 1][j - 1] + 1;
42+
} else {
43+
lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
44+
}
45+
}
46+
}
47+
48+
// Return the length of LCS
49+
return lcs[m][n];
50+
}
51+
52+
// Example usage
53+
const str1 = "ABCDGH";
54+
const str2 = "AEDFHR";
55+
const result = longestCommonSubsequence(str1, str2);
56+
console.log(result); // Output: 3
57+
58+
// Another example
59+
const str3 = "AGGTAB";
60+
const str4 = "GXTXAYB";
61+
const result2 = longestCommonSubsequence(str3, str4);
62+
console.log(result2); // Output: 4
63+
```
64+
65+
## How it works
66+
67+
- The code returns an object with two properties: `length`, which is the length of the longest common subsequence, and `sequence`, which is the actual subsequence itself.
68+
- It also includes a section of code to trace back the 2D array and find the actual LCS string.
69+
- The time complexity of this algorithm is O(mn), where m and n are the lengths of the input strings.
70+
71+
## References
72+
73+
- [Google](https://www.google.com/search?client=opera&q=Longest+Common+Subsequence&sourceid=opera&ie=UTF-8&oe=UTF-8)
74+
75+
## Problem Added By
76+
77+
- [Haris](https://github.com/harisdev-netizen)
78+
79+
## Contributing
80+
81+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
82+
83+
Please make sure to update tests as appropriate.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function longestCommonSubsequence(str1, str2) {
2+
const m = str1.length;
3+
const n = str2.length;
4+
5+
// Initialize a 2D array with 0
6+
const lcs = Array(m + 1)
7+
.fill()
8+
.map(() => Array(n + 1).fill(0));
9+
10+
// Fill the 2D array with LCS lengths
11+
for (let i = 1; i <= m; i++) {
12+
for (let j = 1; j <= n; j++) {
13+
if (str1[i - 1] === str2[j - 1]) {
14+
lcs[i][j] = lcs[i - 1][j - 1] + 1;
15+
} else {
16+
lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
17+
}
18+
}
19+
}
20+
21+
// Find the LCS string by tracing back the 2D array
22+
let i = m;
23+
let j = n;
24+
let lcsStr = "";
25+
26+
while (i > 0 && j > 0) {
27+
if (str1[i - 1] === str2[j - 1]) {
28+
lcsStr = str1[i - 1] + lcsStr;
29+
i--;
30+
j--;
31+
} else if (lcs[i - 1][j] > lcs[i][j - 1]) {
32+
i--;
33+
} else {
34+
j--;
35+
}
36+
}
37+
38+
return {
39+
length: lcs[m][n],
40+
sequence: lcsStr,
41+
};
42+
}
43+
44+
// Example usage
45+
const str1 = "ABCDGH";
46+
const str2 = "AEDFHR";
47+
const result = longestCommonSubsequence(str1, str2);
48+
console.log(result); // Output: { length: 3, sequence: 'ADH' }
49+
50+
// Another example
51+
const str3 = "AGGTAB";
52+
const str4 = "GXTXAYB";
53+
const result2 = longestCommonSubsequence(str3, str4);
54+
console.log(result2); // Output: { length: 4, sequence: 'GTAB' }

0 commit comments

Comments
 (0)