Blind 75 · 2-D Dynamic Programming

Longest Common Subsequence

Medium

Watch the walkthrough (6:36)

Problem

Pick letters from text1, left to right, skipping any you like, so that the same letters can be picked from text2 in the same order. Return how many letters the longest such pick has.
If the two strings share no letter, return 0.

Examples

Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3

Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3

Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0

Example 4:
Input: text1 = "ezupkr", text2 = "ubmrapg"
Output: 2

Constraints

1 <= text1.length, text2.length <= 1000
Both strings consist of lowercase English letters.

Prerequisites

Dynamic programming — being able to say, in one sentence, what a single cell of your table MEANS. Everything else follows from that sentence.
Rolling variables: when a state only looks back one or two steps, the table collapses to a couple of numbers.

How to think about it

1. Diagonal on a Match, Else the Bigger Neighbour Optimal

Intuition

Trying every pick takes far too long, so build the answer up from the strings' beginnings. Make a table with a row for each letter of text1 and a column for each letter of text2: a cell holds the longest pick for text1 up to its row and text2 up to its column, and an extra row and column of 0s stand for an empty string, which shares nothing. Then compare the last letters, the row's and the column's. If they match, that letter can end the pick, so the cell is the diagonal, the cell up and to the left without either letter, plus 1. If they differ, the pick can't end on both, so drop one: the cell above has dropped the row's letter and the cell to the left the column's, and the cell keeps the bigger. Filling row by row, each row from left to right, the three cells a cell reads are always filled first, and the answer waits in the bottom-right cell, with both strings whole. Grabbing the first match you see is the trap: on "arc" and "care", matching care's c with arc's c leaves nothing of arc after it and stops at 1, and the table finds 2, "ar", by skipping that c. Matching last letters extend the diagonal; else keep the bigger neighbour.

Algorithm

1. Make a table with m + 1 rows and n + 1 columns, all starting at 0: row 0 and column 0 stand for an empty string.
2. Go through the rows from 1 to m, and each row's columns from 1 to n.
3. If the row's letter of text1 matches the column's letter of text2, the cell gets the diagonal, the cell up and to the left, plus 1.
4. Otherwise it gets the bigger of the cell above and the cell to its left.
5. Return the bottom-right cell.

Time & Space

Time O(m*n): each cell is filled once, with a single comparison. Space O(m*n): the table stores every cell. Two rows are enough, O(n): a cell only reads its own row and the row above it.

2. Table Over Prefix Pairs Optimal

Intuition

lcs[i][j] means "the longest common subsequence of the first i characters of one string and the first j of the other". With that sentence the two cases are obvious: if those last characters match, they can both be taken, extending the diagonal; if not, drop one character from one side or the other and keep the better. Two rolling rows are enough since a cell only reads the row above and the cell to its left.

Algorithm

1. An empty prefix shares nothing: all zeros.
2. Matching last characters: one plus the diagonal.
3. Otherwise: the better of dropping from either string.
4. The bottom-right cell is the answer.

Time & Space

Time O(m*n). Space O(min(m, n)) with rolling rows.