Given two strings X and Y consisting of N and M characters, the task is to find the longest subsequence of a string X which is a substring of the string Y .
Examples:
Input: X = “ABCD”, Y = “ACDBDCD”Output: ACDExplanation: “ACD” is longest subsequence of X which is substring of Y.
Input: X = A, Y = AOutput: A
Naive Approach: The simplest approach to solve the given problem is to find all the subsequences of the given string X and print that subsequence among all the generated subsequences which is of maximum length and is a substring of Y .
Time Complexity: O(N*M*2N )Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming . The idea is to create a 2D array , dp[][] of dimensions (N + 1)*(M + 1) and the state dp[i][j] is maximum length of subsequence of X[0, i] which is substring of Y[0, j] . Follow the steps below to solve the problem:
Create a 2D array , dp[][] of size N+1 rows and M+1 columns. Initialize the first row and the first column of the matrix with 0 . Fill all the remaining rows as follows:If the value of X[i – 1] is equal to the value of Y[j – 1] , then update the value of dp[i][j] to (1 + dp[i – 1][j – 1]) . Otherwise, update the value of dp[i][j] to dp[i – 1][j] . Store the maximum length of the required sequence in a variable len by iterating the last row in the matrix and store the row and column index of the maximum cell value in variables i and j respectively. Create a variable, say res to store the resultant string and backtrack from the maximum cell value. Iterate until the value of len is greater than 0 , and perform the following steps:If the value of X[i – 1] is equal to the value of Y[j – 1] , then append X[i – 1] to res and decrement the value of len , i and j by 1. Otherwise, decrement the value of i by 1. After completing the above steps, print the string res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string longestSubsequence(string X, string Y)
{
int n = X.size();
int m = Y.size();
vector<vector<int>> mat(n + 1, vector<int>(m + 1));
for(int i = 0; i < n + 1; i++)
{
for(int j = 0; j < m + 1; j++)
{
if (i == 0 || j == 0)
mat[i][j] = 0;
}
}
for(int i = 1; i < n + 1; i++)
{
for(int j = 1; j < m + 1; j++)
{
if (X[i - 1] == Y[j - 1])
{
mat[i][j] = 1 + mat[i - 1][j - 1];
}
else
{
mat[i][j] = mat[i - 1][j];
}
}
}
int len = 0, col = 0;
for(int i = 0; i < m + 1; i++)
{
if (mat[n][i] > len)
{
len = mat[n][i];
col = i;
}
}
string res = "";
int i = n;
int j = col;
while (len > 0)
{
if (X[i - 1] == Y[j - 1])
{
res = X[i - 1] + res;
i--;
j--;
len--;
}
else
{
i--;
}
}
return res;
}
int main()
{
string X = "ABCD";
string Y = "ACDBDCD";
cout << (longestSubsequence(X, Y));
return 0;
}
Java
class GFG {
public static String longestSubsequence(
String X, String Y)
{
int n = X.length();
int m = Y.length();
int[][] mat = new int[n + 1][m + 1];
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < m + 1; j++) {
if (i == 0 || j == 0)
mat[i][j] = 0;
}
}
for (int i = 1;
i < n + 1; i++) {
for (int j = 1;
j < m + 1; j++) {
if (X.charAt(i - 1)
== Y.charAt(j - 1)) {
mat[i][j] = 1
+ mat[i - 1][j - 1];
}
else {
mat[i][j] = mat[i - 1][j];
}
}
}
int len = 0, col = 0;
for (int i = 0; i < m + 1; i++) {
if (mat[n][i] > len) {
len = mat[n][i];
col = i;
}
}
String res = "";
int i = n;
int j = col;
while (len > 0) {
if (X.charAt(i - 1)
== Y.charAt(j - 1)) {
res = X.charAt(i - 1) + res;
i--;
j--;
len--;
}
else {
i--;
}
}
return res;
}
public static void main(String args[])
{
String X = "ABCD";
String Y = "ACDBDCD";
System.out.println(
longestSubsequence(X, Y));
}
}
Python3
def longestSubsequence(X, Y):
n = len(X)
m = len(Y)
mat = [[0 for i in range(m + 1)]
for j in range(n + 1)]
for i in range(0, n + 1):
for j in range(0, m + 1):
if (i == 0 or j == 0):
mat[i][j] = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if (X[i - 1] == Y[j - 1]):
mat[i][j] = 1 + mat[i - 1][j - 1]
else:
mat[i][j] = mat[i - 1][j]
len1 = 0
col = 0
for i in range(0, m + 1):
if (mat[n][i] > len1):
len1 = mat[n][i]
col = i
res = ""
i = n
j = col
while (len1 > 0):
if (X[i - 1] == Y[j - 1]):
res = X[i - 1] + res
i -= 1
j -= 1
len1 -= 1
else:
i -= 1
return res
X = "ABCD"
Y = "ACDBDCD"
print(longestSubsequence(X, Y))
C#
using System;
using System.Collections.Generic;
class GFG{
static string longestSubsequence(string X, string Y)
{
int i, j;
int n = X.Length;
int m = Y.Length;
int [,]mat = new int[n + 1, m + 1];
for(i = 0; i < n + 1; i++)
{
for(j = 0; j < m + 1; j++)
{
if (i == 0 || j == 0)
mat[i,j] = 0;
}
}
for(i = 1; i < n + 1; i++)
{
for(j = 1; j < m + 1; j++)
{
if (X[i - 1] == Y[j - 1])
{
mat[i, j] = 1 + mat[i - 1, j - 1];
}
else
{
mat[i, j] = mat[i - 1, j];
}
}
}
int len = 0, col = 0;
for(i = 0; i < m + 1; i++)
{
if (mat[n,i] > len)
{
len = mat[n,i];
col = i;
}
}
string res = "";
i = n;
j = col;
while (len > 0)
{
if (X[i - 1] == Y[j - 1])
{
res = X[i - 1] + res;
i--;
j--;
len--;
}
else
{
i--;
}
}
return res;
}
public static void Main()
{
string X = "ABCD";
string Y = "ACDBDCD";
Console.Write(longestSubsequence(X, Y));
}
}
Javascript
<script>
function longestSubsequence(X,Y)
{
let n = X.length;
let m = Y.length;
let mat = new Array(n + 1);
for (let i = 0; i < n + 1; i++) {
mat[i]=new Array(m+1);
for (let j = 0; j < m + 1; j++) {
if (i == 0 || j == 0)
mat[i][j] = 0;
}
}
for (let i = 1;
i < n + 1; i++) {
for (let j = 1;
j < m + 1; j++) {
if (X[i-1]
== Y[j-1]) {
mat[i][j] = 1
+ mat[i - 1][j - 1];
}
else {
mat[i][j] = mat[i - 1][j];
}
}
}
let len = 0, col = 0;
for (let i = 0; i < m + 1; i++) {
if (mat[n][i] > len) {
len = mat[n][i];
col = i;
}
}
let res = "";
let i = n;
let j = col;
while (len > 0) {
if (X[i-1]
== Y[j-1]) {
res = X[i-1] + res;
i--;
j--;
len--;
}
else {
i--;
}
}
return res;
}
let X = "ABCD";
let Y = "ACDBDCD";
document.write(longestSubsequence(X, Y));
</script>
Time Complexity: O(N*M)Auxiliary Space: O(N*M)
Last Updated :
06 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...