Longest common subsequence

The LCS problem is the problem of finding the longest subsequence in a given set of sequences. However, elements of subsequence doesn’t have to be a continous part of original sequence (check out sample input and output).

Read more on wikipedia.

Input

a(1)a(2) ... a(n)
b(1)b(2) ... b(m)

a(x), b(x) – elements of given sequences

Sample input

asbaaababa
saababx

Sample output

saabab

Complexity

In the following solution there are two nested loops, so the algorithm runs in Θ(len1 · len2) time.

Source code