Given two strings s1, s2 and K, find the length of the longest subsequence formed by consecutive segments of at least length K.
Input : s1 = aggayxysdfa s2 = aggajxaaasdfa k = 4 Output : 8 Explanation: aggasdfa is the longest subsequence that can be formed by taking consecutive segments, minimum of length 4. Here segments are "agga" and "sdfa" which are of length 4 which is included in making the longest subsequence. Input : s1 = aggasdfa s2 = aggajasdfaxy k = 5 Output : 5 Input: s1 = "aabcaaaa" s2 = "baaabcd" k = 3 Output: 4 Explanation: "aabc" is the longest subsequence that is formed by taking segment of minimum length 3. The segment is of length 4.
Prerequisite : Longest Common Subsequence
Create a LCS array where LCSi, j denotes the length of the longest common subsequence formed by characters of s1 till i and s2 till j having consecutive segments of at least length K. Create a cnt array to count the length of the common segment. cnti, j= cnti-1, j-1+1 when s1[i-1]==s2[j-1]. If characters are not equal then segments are not equal hence mark cnti, j as 0.
When cnti, j>=k, then update the lcs value by adding the value of cnti-a, j-a where a is the length of the segments a<=cnti, j. The answer for the longest subsequence with consecutive segments of at least length k will be stored in lcs[n][m] where n and m are the length of string1 and string2.
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.
- Count ways to increase LCS length of two strings by one
- A Space Optimized Solution of LCS
- Longest Common Increasing Subsequence (LCS + LIS)
- LCS (Longest Common Subsequence) of three strings
- Number of ways to insert a character to increase the LCS by one
- Edit distance and LCS (Longest Common Subsequence)
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Maximum length of segments of 0's and 1's
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Strings formed from given characters without any consecutive repeating characters
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Check if a large number can be divided into two or more segments of equal sum
- Maximum number of segments of lengths a, b and c
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Divide the array in K segments such that the sum of minimums is maximized
- Minimum splits required to convert a number into prime segments
- Count of ways to split a given number into prime segments
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.