# LCS formed by consecutive segments of at least length K

Given two strings s1, s2 and K, find the length of the longest subsequence formed by consecutive segments of at least length K.

Examples:

Input : s1 =aggayxysdfas2 =aggajxaaasdfak = 4 Output : 8 Explanation:aggasdfais 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 = aggasdfas2 = 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 LCS_{i, 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. **cnt _{i, j}= cnt_{i-1, j-1}+1** when s1[i-1]==s2[j-1]. If characters are not equal then segments are not equal hence mark cnt

_{i, j}as 0.

When

**cnt**, then update the lcs value by adding the value of cnt

_{i, j}>=k_{i-a, j-a}where a is the length of the segments

**a<=cnt**. The answer for the longest subsequence with consecutive segments of at least length k will be stored in

_{i, j}**lcs[n][m]**where n and m are the length of string1 and string2.

## C++

`// CPP program to find the Length of Longest ` `// subsequence formed by consecutive segments ` `// of at least length K ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns the length of the longest common subsequence ` `// with a minimum of length of K consecutive segments ` `int` `longestSubsequenceCommonSegment(` `int` `k, string s1, ` ` ` `string s2) ` `{ ` ` ` `// length of strings ` ` ` `int` `n = s1.length(); ` ` ` `int` `m = s2.length(); ` ` ` ` ` `// declare the lcs and cnt array ` ` ` `int` `lcs[n + 1][m + 1]; ` ` ` `int` `cnt[n + 1][m + 1]; ` ` ` ` ` `// initialize the lcs and cnt array to 0 ` ` ` `memset` `(lcs, 0, ` `sizeof` `(lcs)); ` ` ` `memset` `(cnt, 0, ` `sizeof` `(cnt)); ` ` ` ` ` `// iterate from i=1 to n and j=1 to j=m ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `for` `(` `int` `j = 1; j <= m; j++) { ` ` ` ` ` `// stores the maximum of lcs[i-1][j] and lcs[i][j-1] ` ` ` `lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]); ` ` ` ` ` `// when both the characters are equal ` ` ` `// of s1 and s2 ` ` ` `if` `(s1[i - 1] == s2[j - 1]) ` ` ` `cnt[i][j] = cnt[i - 1][j - 1] + 1; ` ` ` ` ` `// when length of common segment is ` ` ` `// more than k, then update lcs answer ` ` ` `// by adding that segment to the answer ` ` ` `if` `(cnt[i][j] >= k) { ` ` ` ` ` `// formulate for all length of segments ` ` ` `// to get the longest subsequence with ` ` ` `// consecutive Common Segment of length ` ` ` `// of min k length ` ` ` `for` `(` `int` `a = k; a <= cnt[i][j]; a++) ` ` ` ` ` `// update lcs value by adding segment length ` ` ` `lcs[i][j] = max(lcs[i][j], ` ` ` `lcs[i - a][j - a] + a); ` ` ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `lcs[n][m]; ` `} ` ` ` `// driver code to check the above fucntion ` `int` `main() ` `{ ` ` ` `int` `k = 4; ` ` ` `string s1 = ` `"aggasdfa"` `; ` ` ` `string s2 = ` `"aggajasdfa"` `; ` ` ` `cout << longestSubsequenceCommonSegment(k, s1, s2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the Length of Longest ` `// subsequence formed by consecutive segments ` `// of at least length K ` ` ` `class` `GFG { ` ` ` ` ` `// Returns the length of the longest common subsequence ` ` ` `// with a minimum of length of K consecutive segments ` ` ` `static` `int` `longestSubsequenceCommonSegment(` `int` `k, String s1, ` ` ` `String s2) ` ` ` `{ ` ` ` `// length of strings ` ` ` `int` `n = s1.length(); ` ` ` `int` `m = s2.length(); ` ` ` ` ` `// declare the lcs and cnt array ` ` ` `int` `lcs[][] = ` `new` `int` `[n + ` `1` `][m + ` `1` `]; ` ` ` `int` `cnt[][] = ` `new` `int` `[n + ` `1` `][m + ` `1` `]; ` ` ` ` ` ` ` `// iterate from i=1 to n and j=1 to j=m ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) { ` ` ` `for` `(` `int` `j = ` `1` `; j <= m; j++) { ` ` ` ` ` `// stores the maximum of lcs[i-1][j] and lcs[i][j-1] ` ` ` `lcs[i][j] = Math.max(lcs[i - ` `1` `][j], lcs[i][j - ` `1` `]); ` ` ` ` ` `// when both the characters are equal ` ` ` `// of s1 and s2 ` ` ` `if` `(s1.charAt(i - ` `1` `) == s2.charAt(j - ` `1` `)) ` ` ` `cnt[i][j] = cnt[i - ` `1` `][j - ` `1` `] + ` `1` `; ` ` ` ` ` `// when length of common segment is ` ` ` `// more than k, then update lcs answer ` ` ` `// by adding that segment to the answer ` ` ` `if` `(cnt[i][j] >= k) ` ` ` `{ ` ` ` ` ` `// formulate for all length of segments ` ` ` `// to get the longest subsequence with ` ` ` `// consecutive Common Segment of length ` ` ` `// of min k length ` ` ` `for` `(` `int` `a = k; a <= cnt[i][j]; a++) ` ` ` ` ` `// update lcs value by adding ` ` ` `// segment length ` ` ` `lcs[i][j] = Math.max(lcs[i][j], ` ` ` `lcs[i - a][j - a] + a); ` ` ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `lcs[n][m]; ` ` ` `} ` ` ` ` ` `// driver code to check the above fucntion ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `k = ` `4` `; ` ` ` `String s1 = ` `"aggasdfa"` `; ` ` ` `String s2 = ` `"aggajasdfa"` `; ` ` ` `System.out.println(longestSubsequenceCommonSegment(k, s1, s2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by prerna saini. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the Length of Longest ` `// subsequence formed by consecutive segments ` `// of at least length K ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns the length of the longest common subsequence ` ` ` `// with a minimum of length of K consecutive segments ` ` ` `static` `int` `longestSubsequenceCommonSegment(` `int` `k, ` `string` `s1, ` ` ` `string` `s2) ` ` ` `{ ` ` ` `// length of strings ` ` ` `int` `n = s1.Length; ` ` ` `int` `m = s2.Length; ` ` ` ` ` `// declare the lcs and cnt array ` ` ` `int` `[,]lcs = ` `new` `int` `[n + 1,m + 1]; ` ` ` `int` `[,]cnt = ` `new` `int` `[n + 1,m + 1]; ` ` ` ` ` ` ` `// iterate from i=1 to n and j=1 to j=m ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `for` `(` `int` `j = 1; j <= m; j++) { ` ` ` ` ` `// stores the maximum of lcs[i-1][j] and lcs[i][j-1] ` ` ` `lcs[i,j] = Math.Max(lcs[i - 1,j], lcs[i,j - 1]); ` ` ` ` ` `// when both the characters are equal ` ` ` `// of s1 and s2 ` ` ` `if` `(s1[i - 1] == s2[j - 1]) ` ` ` `cnt[i,j] = cnt[i - 1,j - 1] + 1; ` ` ` ` ` `// when length of common segment is ` ` ` `// more than k, then update lcs answer ` ` ` `// by adding that segment to the answer ` ` ` `if` `(cnt[i,j] >= k) ` ` ` `{ ` ` ` ` ` `// formulate for all length of segments ` ` ` `// to get the longest subsequence with ` ` ` `// consecutive Common Segment of length ` ` ` `// of min k length ` ` ` `for` `(` `int` `a = k; a <= cnt[i,j]; a++) ` ` ` ` ` `// update lcs value by adding ` ` ` `// segment length ` ` ` `lcs[i,j] = Math.Max(lcs[i,j], ` ` ` `lcs[i - a,j - a] + a); ` ` ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `lcs[n,m]; ` ` ` `} ` ` ` ` ` `// driver code to check the above fucntion ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `k = 4; ` ` ` `string` `s1 = ` `"aggasdfa"` `; ` ` ` `string` `s2 = ` `"aggajasdfa"` `; ` ` ` `Console.WriteLine(longestSubsequenceCommonSegment(k, s1, s2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

**Output:**

8

## Recommended Posts:

- Maximum length of segments of 0's and 1's
- All possible strings of any length that can be formed from a given string
- K length words that can be formed from given characters without repetition
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Find length of the longest consecutive path from a given starting character
- Maximum number of segments of lengths a, b and c
- Check if a large number can be divided into two or more segments of equal sum
- Count of strings that can be formed using a, b and c under given constraints
- Longest substring of 0s in a string formed by k concatenations
- Count of strings that can be formed from another string using each character at-most once
- Lexicographically largest string formed from the characters in range L and R
- Number of balanced bracket expressions that can be formed from a string
- Lexicographically smallest string formed by removing at most one character
- Get K-th letter of the decoded string formed by repeating substrings

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.