Given two string X and Y. The task is to find the length of longest subsequence of string X which is substring in sequence Y.
Input : X = "ABCD", Y = "BACDBDCD" Output : 3 "ACD" is longest subsequence of X which is substring of Y. Input : X = "A", Y = "A" Output : 1
Method 1 (Brute Force):
Use brute force to find all the subsequence of X and for each subsequence check whether it is substring of Y or not. If it is substring of Y, maintain a maximum length variable and compare length with it.
Method 2: (Dynamic Programming):
Let n be length of X and m be length of Y. Create a 2D array ‘dp’ of m + 1 rows and n + 1 columns. Value dp[i][j] is maximum length of subsequence of X[0….j] which is substring of Y[0….i]. Now for each cell of dp fill value as :
for (i = 1 to m) for (j = 1 to n) if (x[i-1] == y[j - 1]) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = dp[i][j-1];
And finally, the length of the longest subsequence of x which is substring of y is max(dp[i][n]) where 1 <= i <= m.
Below is implementation this approach:
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of smallest substring of a given string which contains another string as subsequence
- Length of longest substring to be deleted to make a string equal to another string
- Maximum length prefix of one string that occurs as subsequence in another
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Length of the longest increasing subsequence which does not contain a given sequence as Subarray
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Length of the longest subsegment which is UpDown after inserting atmost one integer
- Minimum length of substring whose rotation generates a palindromic substring
- Find length of longest substring with at most K normal characters
- Longest subsequence with at least one character appearing in every string
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Substring of 1's after removing one character
- Find the length of the longest subsequence with first K alphabets having same frequency
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if the given string is shuffled substring of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Count of substrings of a string containing another given string as a substring