LCS ( Longest Common Subsequence ) of three strings | PHP program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Driver code
< ? php $ X = " AGGT12" ; $ Y = "12TXAYB " ; $ Z = "12XBA " ; $ dp = array_fill ( 0 , 100 , array_fill ( 0 , 100 , array_fill ( 0 , 100 , -1 ) ) ) ; function lcsOf3 ( $ i , $ j , $ k ) { global $ dp , $ X , $ Y , $ Z ; if ( $ i == -1 $ j == -1 $ k == -1 ) return 0 ; if ( $ dp [ $ i ] [ $ j ] [ $ k ] != -1 ) return $ dp [ $ i ] [ $ j ] [ $ k ] ; if ( $ X [ $ i ] == $ Y [ $ j ] && $ Y [ $ j ] == $ Z [ $ k ] ) return $ dp [ $ i ] [ $ j ] [ $ k ] = 1 + lcsOf3 ( $ i - 1 , $ j - 1 , $ k - 1 ) ; else return $ dp [ $ i ] [ $ j ] [ $ k ] = max ( max ( lcsOf3 ( $ i - 1 , $ j , $ k ) , lcsOf3 ( $ i , $ j - 1 , $ k ) ) , lcsOf3 ( $ i , $ j , $ k - 1 ) ) ; } $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; $ o = strlen ( $ Z ) ; echo " Length ▁ of ▁ LCS ▁ is ▁ " . lcsOf3 ( $ m - 1 , $ n - 1 , $ o - 1 ) ; ? >