Printing Longest Common Subsequence | Set 2 ( Printing All ) | Maximum String length ; Returns set containing all LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; construct a set to store possible LCS ; If we reaches end of either String , return a empty set ; If the last characters of X and Y are same ; recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] in the matrix ; append current character to all possible LCS of subString X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] . ; If the last characters of X and Y are not same ; If LCS can be constructed from top side of the matrix , recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 1 ] ; If LCS can be constructed from left side of the matrix , recurse for X [ 0. . m - 1 ] and Y [ 0. . n - 2 ] ; merge two sets if L [ m - 1 ] [ n ] == L [ m ] [ n - 1 ] Note s will be empty if L [ m - 1 ] [ n ] != L [ m ] [ n - 1 ] ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Build L [ m + 1 ] [ n + 1 ] in bottom up fashion ; Driver Code
let N = 100 ; let L = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { L [ i ] = new Array ( N ) ; } function findLCS ( X , Y , m , n ) { let s = new Set ( ) ; if ( m == 0 n == 0 ) { s . add ( " " ) ; return s ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { let tmp = findLCS ( X , Y , m - 1 , n - 1 ) ; for ( let str of tmp . values ( ) ) s . add ( str + X [ m - 1 ] ) ; } else { if ( L [ m - 1 ] [ n ] >= L [ m ] [ n - 1 ] ) s = findLCS ( X , Y , m - 1 , n ) ; if ( L [ m ] [ n - 1 ] >= L [ m - 1 ] [ n ] ) { let tmp = findLCS ( X , Y , m , n - 1 ) ; for ( let item of tmp . values ( ) ) s . add ( item ) } } return s ; } function LCS ( X , Y , m , n ) { for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; document . write ( " " + LCS ( X , Y , m , n ) + " " ) ; let s = findLCS ( X , Y , m , n ) ; for ( let str of s . values ( ) ) document . write ( str + " " ) ;