Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 3, 2019 21:17
Show Gist options
  • Select an option

  • Save imedadel/362dba800434dfd6983c60a8e2b1e495 to your computer and use it in GitHub Desktop.

Select an option

Save imedadel/362dba800434dfd6983c60a8e2b1e495 to your computer and use it in GitHub Desktop.
frstWord = list(input().strip())
scndWord = list(input().strip())
def longestChild(frst, scnd):
grid = [[0 for _ in range(len(scnd)+1)] for _ in range(len(frst)+1)]
result = 0
for i in range(len(frst)+1):
for j in range(len(scnd)+1):
if i == 0 or j == 0:
grid[i][j] = 0
elif frst[i-1] == scnd[j-1]:
grid[i][j] = grid[i-1][j-1] + 1
result = max(result, grid[i][j])
else:
grid[i][j] = max(grid[i-1][j], grid[i][j-1])
return result
print(longestChild(frstWord, scndWord))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment