Write a recursive method lcs that takes two
String arguments and returns a longest common subsequence
of the two as a String. No loops. The signature is:
public String lcs(String a, String b)
Don’t worry about combinatorial explosion yet; just get the recursion working. This one is only tested on strings of up to ten characters.
Write a method fastLCS with the same arguments and return
type as lcs that also returns a longest common subsequence
but uses dynamic programming so it doesn’t take forever on longer
strings. This one is tested on strings of 30 to 60 characters, far more
than your lcs could get through before the tests time out.
The tests count how many method calls your code makes and fail any test
case that makes far more than a dynamic programming solution needs. So
just calling lcs isn’t going to work.
A subsequence of a string is what’s left after you delete zero or
more of its characters without rearranging the ones that remain. So
"ACE" is a subsequence of "ABCDE" but
"AEC" is not. Unlike a substring, a subsequence doesn’t have
to be contiguous.
A longest common subsequence (LCS) of two strings is the longest
string that is a subsequence of both of them. For example an LCS of
"AGGTAB" and "GXTXAYB" is "GTAB".
Among other things, this is how tools like diff figure out
what changed between two versions of a file and how biologists compare
DNA sequences.
Two strings can have more than one LCS: "AC" and
"CA" have two, "A" and "C". When
there’s more than one, your methods can return any of them. And if the
strings have nothing in common, the LCS is the empty string.
You’ll write this twice: first as a straightforward recursion that works fine but only on short strings, and then again using dynamic programming so it’s fast enough for long ones.