The Wayback Machine - https://web.archive.org/web/20201006212347/https://github.com/TheAlgorithms/Python/pull/2910/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Least Common Subsequence #2910

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
@@ -0,0 +1,30 @@
# Dynamic Programming implementation of LCS problem

def lcs(X, Y):
# find the length of the strings
m = len(X)
n = len(Y)

# declaring the array for storing the dp values
L = [[None]*(n + 1) for i in range(m + 1)]


for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0 :
L[i][j] = 0
elif X[i-1] == Y[j-1]:
L[i][j] = L[i-1][j-1]+1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])

# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
return L[m][n]
# end of function lcs


#Running lcs
X = "AGGTAB"
Y = "GXTXAYB"
print("Length of LCS is ", lcs(X, Y))

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.