You are viewing a single comment's thread. Return to all comments →
``` public static int Rec(string s1,string s2,int[][] memo,int i1,int i2){ if(i1<0||i2<0) return 0; if(memo[i1][i2]!=-1) return memo[i1][i2]; if(s1[i1]==s2[i2]){ memo[i1][i2]=1+Rec(s1,s2,memo,i1-1,i2-1); return memo[i1][i2]; }else { memo[i1][i2]=Math.Max(Rec(s1,s2,memo,i1,i2-1),Rec(s1,s2,memo,i1-1,i2)); return memo[i1][i2]; } } public static int commonChild(string s1, string s2) { int[][] dp=new int[s1.Length][]; for(int i=0;i<s1.Length;i++) dp[i]=new int[s2.Length]; for(int i=0;i<s1.Length;i++){ for(int j=0;j<s2.Length;j++){ dp[i][j]=-1; } } Rec(s1,s2,dp,s1.Length-1,s2.Length-1); return dp[s1.Length-1][s2.Length-1]; }
`
This is a recusive aproach with memoization.
Seems like cookies are disabled on this browser, please enable them to open this website
Common Child
You are viewing a single comment's thread. Return to all comments →
`