• + 0 comments

    ` def commonChild(s1, s2): # Write your code here matrix = [[0 for j in range(len(s2))] for i in range(len(s1))]

    if s1[0] == s2[0]:
        matrix[0][0] = 1
    else:
        matrix[0][0] = 0
    for j in range(1, len(s2)):
        if s1[0] == s2[j]:
            matrix[0][j] = 1
        else:
            matrix[0][j] = matrix[0][j-1]
    for i in range(1, len(s1)):
        if s2[0] == s1[i]:
            matrix[i][0] = 1
        else:
            matrix[i][0] = matrix[i-1][0]
    
    for i in range(1, len(s1)):
        for j in range(1, len(s2)):
            if s1[i] == s2[j]:
                matrix[i][j] = 1 + matrix[i-1][j-1]
            else:
                matrix[i][j] = max(matrix[i][j-1], matrix[i-1][j])
    
    return matrix[len(s1)-1][len(s2)-1]