Common Child

  • + 0 comments
        public static int commonChild(String s1, String s2) {
        // Write your code here
        int[][] dp = new int[5001][5001];
        for (int i = 0; i < s1.length(); i++){
            for (int j = 0; j < s2.length(); j++){
                if (s1.charAt(i) == s2.charAt(j)){
                    dp[i+1][j+1] = dp[i][j]+1;
                    
                }
                else{
                    dp[i+1][j+1] = Math.max(dp[i][j+1],dp[i+1][j]);
                }
                
            }
        }
        return dp[s1.length()][s2.length()];
        }