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 = 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 lcsi-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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubsequenceCommonSegment( int k, string s1,
string s2)
{
int n = s1.length();
int m = s2.length();
int lcs[n + 1][m + 1];
int cnt[n + 1][m + 1];
memset (lcs, 0, sizeof (lcs));
memset (cnt, 0, sizeof (cnt));
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= m; j++) {
lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
if (s1[i - 1] == s2[j - 1])
cnt[i][j] = cnt[i - 1][j - 1] + 1;
if (cnt[i][j] >= k) {
for ( int a = k; a <= cnt[i][j]; a++)
lcs[i][j] = max(lcs[i][j],
lcs[i - a][j - a] + a);
}
}
}
return lcs[n][m];
}
int main()
{
int k = 4;
string s1 = "aggasdfa" ;
string s2 = "aggajasdfa" ;
cout << longestSubsequenceCommonSegment(k, s1, s2);
return 0;
}
|
Java
class GFG {
static int longestSubsequenceCommonSegment( int k, String s1,
String s2)
{
int n = s1.length();
int m = s2.length();
int lcs[][] = new int [n + 1 ][m + 1 ];
int cnt[][] = new int [n + 1 ][m + 1 ];
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= m; j++) {
lcs[i][j] = Math.max(lcs[i - 1 ][j], lcs[i][j - 1 ]);
if (s1.charAt(i - 1 ) == s2.charAt(j - 1 ))
cnt[i][j] = cnt[i - 1 ][j - 1 ] + 1 ;
if (cnt[i][j] >= k)
{
for ( int a = k; a <= cnt[i][j]; a++)
lcs[i][j] = Math.max(lcs[i][j],
lcs[i - a][j - a] + a);
}
}
}
return lcs[n][m];
}
public static void main(String[] args)
{
int k = 4 ;
String s1 = "aggasdfa" ;
String s2 = "aggajasdfa" ;
System.out.println(longestSubsequenceCommonSegment(k, s1, s2));
}
}
|
Python3
def longestSubsequenceCommonSegment(k, s1, s2) :
n = len (s1)
m = len (s2)
lcs = [[ 0 for x in range (m + 1 )] for y in range (n + 1 )]
cnt = [[ 0 for x in range (m + 1 )] for y in range (n + 1 )]
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , m + 1 ) :
lcs[i][j] = max (lcs[i - 1 ][j], lcs[i][j - 1 ])
if (s1[i - 1 ] = = s2[j - 1 ]):
cnt[i][j] = cnt[i - 1 ][j - 1 ] + 1 ;
if (cnt[i][j] > = k) :
for a in range (k, cnt[i][j] + 1 ) :
lcs[i][j] = max (lcs[i][j],lcs[i - a][j - a] + a)
return lcs[n][m]
k = 4
s1 = "aggasdfa"
s2 = "aggajasdfa"
print (longestSubsequenceCommonSegment(k, s1, s2))
|
C#
using System;
class GFG {
static int longestSubsequenceCommonSegment( int k, string s1,
string s2)
{
int n = s1.Length;
int m = s2.Length;
int [,]lcs = new int [n + 1,m + 1];
int [,]cnt = new int [n + 1,m + 1];
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= m; j++) {
lcs[i,j] = Math.Max(lcs[i - 1,j], lcs[i,j - 1]);
if (s1[i - 1] == s2[j - 1])
cnt[i,j] = cnt[i - 1,j - 1] + 1;
if (cnt[i,j] >= k)
{
for ( int a = k; a <= cnt[i,j]; a++)
lcs[i,j] = Math.Max(lcs[i,j],
lcs[i - a,j - a] + a);
}
}
}
return lcs[n,m];
}
public static void Main()
{
int k = 4;
string s1 = "aggasdfa" ;
string s2 = "aggajasdfa" ;
Console.WriteLine(longestSubsequenceCommonSegment(k, s1, s2));
}
}
|
Javascript
<script>
function longestSubsequenceCommonSegment(k, s1, s2)
{
var n = s1.length;
var m = s2.length;
var lcs = Array.from(Array(n+1), ()=>Array(m+1).fill(0));
var cnt = Array.from(Array(n+1), ()=>Array(m+1).fill(0));
for ( var i = 1; i <= n; i++) {
for ( var j = 1; j <= m; j++) {
lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
if (s1[i - 1] == s2[j - 1])
cnt[i][j] = cnt[i - 1][j - 1] + 1;
if (cnt[i][j] >= k) {
for ( var a = k; a <= cnt[i][j]; a++)
lcs[i][j] = Math.max(lcs[i][j],
lcs[i - a][j - a] + a);
}
}
}
return lcs[n][m];
}
var k = 4;
var s1 = "aggasdfa" ;
var s2 = "aggajasdfa" ;
document.write( longestSubsequenceCommonSegment(k, s1, s2));
</script>
|
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(N*M), as we are using extra space for lcs and cnt.
Where N and M are the length of the strings.