Skip to content

Commit 04ff72f

Browse files
authored
Merge pull request #1579 from Nikhil11200/problem_1143
Added solution to Leetcode problem 1143
2 parents a1fa1e1 + 146f8fb commit 04ff72f

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
id: longest common subsequence
3+
title: longest common subsequence
4+
sidebar_label: 1143-longest common subsequence
5+
tags:
6+
- Recursion
7+
- Dynamic Programming
8+
- Java
9+
- Cpp
10+
- Python
11+
description: "Given two strings , return the longest common subsequence ."
12+
---
13+
14+
## Problem
15+
16+
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
17+
18+
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
19+
20+
For example, "ace" is a subsequence of "abcde".
21+
A common subsequence of two strings is a subsequence that is common to both strings.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
**Input:** text1 = "abcde", text2 = "ace"
28+
**Output:** 3
29+
**Explanation:** The longest common subsequence is "ace" and its length is 3.
30+
31+
**Example 2:**
32+
33+
**Input:** text1 = "abc", text2 = "abc"
34+
**Output:** 3
35+
**Explanation:** The longest common subsequence is "abc" and its length is 3.
36+
37+
### Constraints
38+
39+
- `1 <= text1.length, text2.length <= 1000`
40+
- `text1` and `text2` consist of only lowercase English characters.
41+
42+
43+
44+
---
45+
46+
## Approach
47+
48+
To count the length of the longest common subsequence , will make 2 pointers pointing at the end of the two texts then will compare the characters at that index whether it is same or not then if not equal then decrement both otherwise return the max when decrementing both the pointers one by one.
49+
50+
### Steps:
51+
52+
1. Create a 2D table dp of size (m+1) x (n+1), where m and n are the lengths of the two input strings. Initialize all elements in the table to -1, which will serve as a flag to indicate that no result has been stored yet.
53+
54+
2. Fill the Memoization Table
55+
56+
Iterate through the table and fill it with the results of subproblems. For each cell dp[i][j], consider the following cases:
57+
58+
If i or j is 0, the longest common subsequence is 0 (since one of the strings is empty).
59+
If the current characters at positions i-1 and j-1 in the two strings are the same, then the longest common subsequence is one more than the longest common subsequence of the substrings up to i-1 and j-1.
60+
Otherwise, the longest common subsequence is the maximum of the longest common subsequences of the substrings up to i-1 and j, and the substrings up to i and j-1.
61+
3. Read the Memoization Table
62+
63+
The value at dp[m][n] represents the length of the longest common subsequence. You can use this value to construct the actual subsequence by tracing back the table from dp[m][n] to dp[0][0].
64+
65+
4. Construct the Longest Common Subsequence
66+
67+
To construct the longest common subsequence, start from the bottom-right corner of the table and move diagonally up-left if the current characters match, or move up or left if they don’t. When you reach a cell with a value of -1, stop and backtrack to the previous cell.
68+
69+
### Solution
70+
71+
#### Java Solution
72+
73+
```java
74+
import java.util.Arrays;
75+
76+
class Solution {
77+
78+
static final int maximum = 1000;
79+
80+
static int lcs(String X, String Y, int m, int n, int dp[][]) {
81+
if (m == 0 || n == 0) {
82+
return 0;
83+
}
84+
85+
if (dp[m - 1][n - 1] != -1) {
86+
return dp[m - 1][n - 1];
87+
}
88+
89+
if (X.charAt(m - 1) == Y.charAt(n - 1)) {
90+
91+
dp[m - 1][n - 1] = 1 + lcs(X, Y, m - 1, n - 1, dp);
92+
93+
return dp[m - 1][n - 1];
94+
} else {
95+
96+
dp[m - 1][n - 1] = Math.max(lcs(X, Y, m, n - 1, dp),
97+
lcs(X, Y, m - 1, n, dp));
98+
99+
return dp[m - 1][n - 1];
100+
}
101+
}
102+
}
103+
104+
```
105+
### C++ Solution
106+
107+
```cpp
108+
#include <bits/stdc++.h>
109+
using namespace std;
110+
111+
const int maximum = 1000;
112+
int lcs(string X, string Y, int m, int n, int dp[][maximum])
113+
{
114+
if (m == 0 || n == 0)
115+
return 0;
116+
117+
if (dp[m - 1][n - 1] != -1)
118+
return dp[m - 1][n - 1];
119+
120+
if (X[m - 1] == Y[n - 1]) {
121+
122+
dp[m - 1][n - 1] = 1 + lcs(X, Y, m - 1, n - 1, dp);
123+
124+
return dp[m - 1][n - 1];
125+
}
126+
else {
127+
128+
dp[m - 1][n - 1] = max(lcs(X, Y, m, n - 1, dp),
129+
lcs(X, Y, m - 1, n, dp));
130+
131+
return dp[m - 1][n - 1];
132+
}
133+
}
134+
```
135+
### Python Solution
136+
137+
```python
138+
maximum = 1000
139+
def lcs(X, Y, m, n, dp):
140+
141+
if (m == 0 or n == 0):
142+
return 0
143+
144+
if (dp[m - 1][n - 1] != -1):
145+
return dp[m - 1][n - 1]
146+
147+
if (X[m - 1] == Y[n - 1]):
148+
149+
dp[m - 1][n - 1] = 1 + lcs(X, Y, m - 1, n - 1, dp)
150+
151+
return dp[m - 1][n - 1]
152+
153+
else :
154+
155+
dp[m - 1][n - 1] = max(lcs(X, Y, m, n - 1, dp),
156+
lcs(X, Y, m - 1, n, dp))
157+
158+
return dp[m - 1][n - 1]
159+
```
160+
161+
### Complexity Analysis
162+
**Time Complexity:** O(m * n)
163+
>where n and m are lengths of the first and second string respectively.
164+
165+
**Space Complexity:** O(n*m)
166+
>Reason: We are using the extra space in the form of dp array.
167+
168+
### References
169+
**LeetCode Problem:** Longest Common Subsequence

0 commit comments

Comments
 (0)