Memoization ( 1D , 2D and 3D ) | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; base case ; if equal , then check for next combination ; recursive call ; return the maximum of the three other possible states in recursion ; Driver code
function lcs ( X , Y , Z , m , n , o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { return 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; } else { return Math . max ( lcs ( X , Y , Z , m , n - 1 , o ) , Math . max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; } } let X = " " . split ( " " ) ; let Y = " " . split ( " " ) ; let Z = " " . split ( " " ) ; let m = X . length ; let n = Y . length ; let o = Z . length ; document . write ( " " + lcs ( X , Y , Z , m , n , o ) ) ;