This type of problem can be solved easily using Dynamic Programming. In this problem, you are generally given two sequences and you need to find the length of the common subsequence present in them. These type of problem finds usage in bioinformatics.
A sub-sequence may be defined as the sequence that appear in relatively same order but not necessarily continuous. E.g.”abd” and “abc” are subsequences of “abdcfgrhd” .
###################### longest common subsequence ######################## def solve(x,y): len_x = len(x) len_y = len(y) # creating a Matrix mat = [[0] * (len_y) for i in range(len_x)] sequence = [] for i in range(len_x): for j in range(len_y): if x[i] == y [j]: mat[i][j] = mat[i-1][j-1] + 1 sequence.append(x[i]) else: mat[i][j] = max(mat[i - 1][j], mat[i][j - 1]) return mat[len_x-1][len_y-1] if __name__ == "__main__": x = 'abrxrxr' y = 'abtdrrxxr' print(solve(x,y))
In Python, the print() function is a fundamental tool for displaying output. While printing simple…
Python is a versatile programming language known for its simplicity and flexibility. When working on…
PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…
PDF (Portable Document Format) files are widely used for document exchange due to their consistent…
Python is a high-level programming language known for its simplicity and ease of use. However,…
Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…
This website uses cookies.