Common Child

Sort by

recency

|

15 Discussions

|

  • + 0 comments

    Java O(n m)

    public static int commonChild(String s1, String s2) {
        int n = s1.length();
        int m = s2.length();
        
        int[][] dp = new int[n+1][m+1];
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; 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+1][j], dp[i][j+1]);
                }
            }
        }
        return dp[n][m];
    }
    
  • + 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()];
        }
    
  • + 0 comments
    def commonChild(s1, s2):
        n = len(s1)
        
        # Create a 2D array to store the lengths of the common child
        dp = [[0] * (n + 1) for _ in range(n + 1)]
        
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                if s1[i - 1] == s2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1] + 1
                else:
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
        
        return dp[n][n]
    
  • + 1 comment

    I could not understand this test case in the Run: 'SHINCHAN' vs 'NOHARAAA'. I can only see HA as the common child, so my anwser is 2 instead of the offical answer 3.

    Attached is my code in Python3:

    def commonChild(s1, s2):
        '''
        find the closest match from beginning
        '''
        for i in range(0, min(len(s1), len(s2))):
            for j in range(i+1):
                if s1[i] == s2[j]:
                    cnt = 1
                    for k in range(1, min(len(s1) - i, len(s2) - j)):
                        if s1[i+k] != s2[j+k]:
                            break
                        cnt += 1
                    return cnt + commonChild(s1[i+cnt:], s2[j+cnt:])
                if s1[j] == s2[i]:
                    cnt = 1
                    for k in range(1, min(len(s1) - j, len(s2) - i)):
                        if s1[j+k] != s2[i+k]:
                            break
                        cnt += 1
                    return cnt + commonChild(s1[j+cnt:], s2[i+cnt:])
        return 0
    
  • + 0 comments

    Java8

    public static int commonChild(String s1, String s2) {
        //watch lecture at 
        //https://www.youtube.com/watch?v=jHGgXV27qtk
        //to understand how to solve 
        //this problem using dynamic programming
        char c1;
        char c2;
        int max;
        
        int n = s1.length();
        int[][] memo = new int[n + 1][n + 1];
        
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < n + 1; j ++) {
                if (i == 0 || j == 0) {
                    memo[i][j] = 0;
                }
                else {
                    c1 = s1.charAt(i - 1);
                    c2 = s2.charAt(j - 1);
                    
                    if (c1 != c2) {
                        max = Math.max(memo[i - 1][j], memo[i][j - 1]);
                        memo[i][j] = max;
                    }
                    else {
                        memo[i][j] = memo[i - 1][j - 1] + 1;
                    }
                }
            }
        }
        return memo[n][n];
        /* GETTING THE LONGEST COMMON STRING
        int i = n;
        int j = n;
        
        LinkedList<Character> commonString = new LinkedList<>();
        
        while (memo[i][i] != 0) {
            char c1 = s1.charAt(i - 1);
            char c2 = s2.charAt(j - 1);
            
            if (c1 == c2) {
                commonString.addFirst(c1);
                i--;
                j--;
            }
            else if (memo[i][j - 1] >= memo[i - 1][j]) {
                j--;
            } 
            else {
                i--;
            }
        }
        
        StringBuilder builder = new StringBuilder();
        for (Character c : commonString) {
            builder.appends(c);
        }
        
        String common = builder.toString();
        */