A longest common subsequence of two finite sequences has maximum length among sequences that occur in both with their original order. Characters can be skipped; this differs from requiring a contiguous substring. For and , is a common subsequence of length four.
Let be the optimum for the first and terms. Set . If the final terms agree, ; otherwise . Filling the table gives time. Retaining predecessor choices reconstructs an actual subsequence, while two rows suffice if only the length is needed.

Articles by others on the same topic (2)