We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
`
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]
Cookie support is required to access HackerRank
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 →
` def commonChild(s1, s2): # Write your code here matrix = [[0 for j in range(len(s2))] for i in range(len(s1))]