Find length of longest subsequence of one string which is substring of another string | Javascript program to find maximum length of subsequence of a string X such it is substring in another string Y . ; Return the maximum size of substring of X which is substring in Y . ; Initialize the dp [ ] [ ] to 0. ; Calculating value for each element . ; If alphabet of string X and Y are equal make dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; Else copy the previous value in the row i . e dp [ i - 1 ] [ j - 1 ] ; Finding the maximum length . ; Driver Program
var MAX = 1000 ; function maxSubsequenceSubstring ( x , y , n , m ) { var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) ) ; for ( var i = 0 ; i <= m ; i ++ ) for ( var j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( var i = 1 ; i <= m ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( x [ j - 1 ] == y [ i - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } } var ans = 0 ; for ( var i = 1 ; i <= m ; i ++ ) ans = Math . max ( ans , dp [ i ] [ n ] ) ; return ans ; } var x = " " ; var y = " " ; var n = x . length , m = y . length ; document . write ( maxSubsequenceSubstring ( x , y , n , m ) ) ;