LCS ( Longest Common Subsequence ) of three strings | C # 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
using System ; class GFG { static string X = " AGGT12" ; static string Y = "12TXAYB " ; static string Z = "12XBA " ; static int [ , , ] dp = new int [ 100 , 100 , 100 ] ; static int lcsOf3 ( int i , int j , int k ) { 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 ] = Math . Max ( Math . Max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) ) ; } static void Main ( ) { for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 100 ; j ++ ) for ( int k = 0 ; k < 100 ; k ++ ) dp [ i , j , k ] = - 1 ; int m = X . Length ; int n = Y . Length ; int o = Z . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( m - 1 , n - 1 , o - 1 ) ) ; } }