Memoization ( 1D , 2D and 3D ) | A Naive recursive implementation of LCS problem ; Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code
import java . io . * ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int lcs ( String X , String Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . length ( ) ; int n = Y . length ( ) ; System . out . print ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , m , n ) ) ; } }