Dynamic Programming

Longest Common Subsequence (LCS)

Problem Formulation

  • Input: Arrays and of characters
  • Output: The maximum length of a common subsequence to and (subsequences do not need to be contiguous)

We break this down into a subproblems indexed at , where we want to find the LCS of and . We notice that there are 3 cases:

  1. If , then the longest common subsequence must include these letter (since they are the last letters). Other common letters can be found in and
  2. Else, we have that . If is not in the subsequence, then we search for and . Else if is not in the subsequence, we search in and .

For our base cases, we have and (longest subsequence of empty string is 0).

This i