I've designed an algorithm to find the longest common subsequence. these
These are steps:
This is an example:
X=A, B, C, B, D, A, B
Y=B, D, C, A, B, A
- Pick
A
in the first string. - Look for
A
inY
. - Now that there is an
A
in the second string, append it tocommon_subsequence
. - Return to the first string and pick the next letter that is
B
. - Look for
B
in the second string this time starting from the position ofA
. - There is a
B
afterA
so append B tocommon_subsequence
. - Now pick the next letter in the first string that is
C
. There isn't aC
next toB
in the second string. So assign the value of common_subsequence tolcs
because its length is greater than the length oflcs
.
Pick A
in the first string.
Look for A
in Y
.
Now that there is an A
in the second string, append it to common_subsequence
.
Return to the first string and pick the next letter that is B
.
Look for B
in the second string this time starting from the position of A
.
There is a B
after A
so append B to common_subsequence
.
Now pick the next letter in the first string that is C
. There isn't a C
next to B
in the second string. So assign the value of common_subsequence to lcs
because its length is greater than the length of lcs
.
repeatRepeat the previous steps until reaching the end of the first string. In the end the value of lcs
is the Longest Common Subsequence.
The complexity of this algorithm is theta(n*m)\$\theta(n*m)\$.